jQuery – 使用按钮禁用和启用表单元素

嘿所有,先谢谢你的帮助。 这是我的代码:

$(document).ready(function () { $("#showexistingcust").button().click(function () { $('#newcust :input').attr('disabled', 'disabled'); $('#newcust :select').attr('disabled', 'disabled'); $('#existingcust :input').removeAttr('disabled'); $('#existingcust :select').removeAttr('disabled'); }) $("#showaddcust").button().click(function () { $('#existingcust :input').attr('disabled', 'disabled'); $('#existingcust :select').attr('disabled', 'disabled'); $('#newcust :input').removeAttr('disabled'); $('#newcust :select').removeAttr('disabled'); }) }); 

我对jquery很新,所以我可能会遗漏一些基本的东西。 但这似乎有一些奇怪的行为。 根据我对代码的命令,removeAttr函数要么根本不运行,要么只运行一个按钮/ div。

我不知道为什么会这样,任何人都可以解释一下?

编辑:HTML:

 











Cash Credit Card


您可以通过省略对select元素的操作来简化此操作:输入选择器匹配select以及inputtextareabutton元素。 尝试从处理程序返回false以防止它采取默认操作(我假设这些是锚标记被制作成按钮)。

 $(document).ready(function () { $("#showexistingcust").button().click(function () { $('#newcust :input').attr('disabled', 'disabled'); $('#existingcust :input').removeAttr('disabled'); return false; }) $("#showaddcust").button().click(function () { $('#existingcust :input').attr('disabled', 'disabled'); $('#newcust :input').removeAttr('disabled'); return false; }) });