我有 2 个配对的输入字段,需要在它们之间切换启用/禁用(如果启用其中一个,则应禁用另一个)。
我不知道如何解决这个问题,有什么想法吗?
最佳答案
示例:item1
已启用,item2
已禁用
<input type="text" id="item1">
<input type="text" id="item2" disabled>
<button id="switch">Click here to toggle</button>
当单击某种开关时,这将切换项目。在任何可以切换它的地方使用它,即上面的按钮:
$("#switch").on("click", function()
{
$("#item1").prop("disabled", !$("#item1").prop("disabled"));
$("#item2").prop("disabled", !$("#item2").prop("disabled"));
});
如果你真的想确定它们是对立的;
$("#switch").on("click", function()
{
var toggle = $("#item1").prop("disabled");
$("#item1").prop("disabled", !toggle );
$("#item2").prop("disabled", toggle ); // item2 will always be the opposite of item1
});
关于jQuery 在配对输入上切换禁用/启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12213964/