我正在尝试编写一个简单的jquery插件,在其中-它会有一个textarea或textbox和一些按钮。
单击按钮后,我需要获取相关文本区域或文本框的值。
我已经写了一些代码,但是我陷入了困境。谁能帮我这个?
这是小提琴: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 = $("<div class='outer' id='" + settings.id + "'>Sample Class</div>");
this.textbox = $("<input type='text' name='sample' id='sample' />");
this.textarea = $("<textarea name='sampletextarea' id='sampletextarea'></textarea>");
this.go = $("<input type='button' name='" + settings.id + "_go' id='" + settings.id + "_go' value='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));
您需要修复html,而不要重复div ID。将第二个div更改为:
<div id="example-widget-container1"></div>
其余代码看起来不错。至少就示例而言。
同样,在单击处理程序中,您将不得不根据您的settings.multi变量来提醒textarea和textbox值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句