我正在尝试编写一个简单的jquery插件,其中包含一个textarea或textbox和一些按钮.
单击按钮我需要获取相关textarea或文本框的值.
我写了一些代码,但我陷入了困境.谁能帮我这个?
这是小提琴:http://jsfiddle.net/655wz/
(function ($) { $.fn.widgetify = function (options) { var defaults = { multi: false }; var settings = $.extend({}, defaults, options); return this.each(function () { //create all dom elements here this.template = $("Sample Class"); this.textbox = $(""); this.textarea = $(""); this.go = $(""); //append all dom elements here if (settings.multi) { $(this.template).append(this.textarea); } else { $(this.template).append(this.textbox); } $(this.template).append(this.go); $(this).append(this.template); //all events stuff here this.go.on('click', $.proxy(function () { //use original 'this' alert($(this.textarea).val()); }, this)); return this; }); } }(jQuery));
uchamp.. 5
你需要修复你的HTML,而不是重复你的div id.将第二个div更改为:
其余的代码看起来很好.至少就这个例子而言.
此外,在您的单击处理程序中,您必须根据您的settings.multi变量提醒textarea和文本框值.
你需要修复你的HTML,而不是重复你的div id.将第二个div更改为:
<div id="example-widget-container1"></div>
其余的代码看起来很好.至少就这个例子而言.
此外,在您的单击处理程序中,您必须根据您的settings.multi变量提醒textarea和文本框值.