JQuery脚本加载时序
如果我有一个设置jquery脚本的按钮,有没有办法确保按钮处于非活动状态,直到脚本完成?
在脚本开头的某处(可能是按钮的单击事件),将按钮的disabled属性设置为true:
$("#mybutton").attr("disabled", true);
然后,完成后,删除该属性:
$("#mybutton").removeAttr("disabled");
编辑:
如果您想(略微)想要,请在工作时更改按钮的文本。 如果是图像按钮,您可以将src更改为友好的“请稍候”消息。 这是按钮文本版本的示例:
$("#mybutton").click(function() { var origVal = $(this).attr("value"); $(this).attr("value", "Please wait..."); $(this).attr("disabled", true); //Do your processing. $(this).removeAttr("disabled"); $(this).attr("value", origVal); });
这是我喜欢扩展jQuery的一个领域:
$.fn.disable = function() { return this.each(function() { if (typeof this.disabled != "undefined") this.disabled = true; }); } $.fn.enable = function() { return this.each(function() { if (typeof this.disabled != "undefined") this.disabled = false; }); }
然后你可以这样做:
$("#button").disable(); $("#button").enable();
我发现自己很多都禁用/启用控件。
谢谢cletus为您的function。 我已经使用了你的函数并创建了自己的函数来切换禁用的元素。
$.fn.toggleDisable = function() { return this.each(function() { if (typeof this.disabled != "undefined"){ if(this.disabled){ this.disabled = false; }else{ this.disabled = true; } } }); }