在 jquery 中启用或禁用表单元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4965972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable or disable form elements in jquery
提问by Wern Ancheta
How do I properly disable and enable form elements using jquery. I need to disable the text form element when I click on the select. And vise versa.
如何使用 jquery 正确禁用和启用表单元素。当我点击选择时,我需要禁用文本表单元素。反之亦然。
<html>
<head>
<script src="jq.js"></script>
<script>
$(function(){
$('#fromdate').click(function(){
$('#yosh').attr('disabled','disabled');
$('#fromdate').removeAttr('disabled');
});
$('#yosh').click(function(){
$('#yosh').removeAttr('disabled');
$('#fromdate').attr('disabled','disabled');
});
});
</script>
</head>
<body>
Sort by:
<select name="yosh" id="yosh">
<option value="daily">daily</option>
<option value="weekly">yesterday</option>
<option value="weekly">weekly</option>
<option value="monthly">monthly</option>
<option value="yearly">yearly</option>
</select><br/>
date range:<br/>
From:<input type="text" value="" name="fromdate" id="fromdate"></input><br/>
To:<input type="text" value="" name="todate" id="todate"></input><br/>
Customer:<input type="text" value="" name="customer" id="customer"></input>
</body>
</html>
回答by Sang Suantak
Example of what i've written in the comment:
我在评论中写的例子:
<span id="spnSel">
<select name="yosh" id="yosh">
<option value="daily">daily</option>
<option value="weekly">yesterday</option>
<option value="weekly">weekly</option>
<option value="monthly">monthly</option>
<option value="yearly">yearly</option>
</select>
</span>
Add this event:
添加此事件:
$('#spnSel').mouseover(function () {
$('#yosh').prop('disabled', false);
});
You can do this for the text boxes as well. This may not be the best solution/approach but it will do the job.
您也可以对文本框执行此操作。这可能不是最好的解决方案/方法,但它可以完成工作。
回答by jondavidjohn
the syntax is
语法是
$('formelement').attr('disabled',true);
or to re-enable
或重新启用
$('formelement').removeAttr('disabled');
回答by Vivek
look at this...
看这个...
<script>
$(function(){
$('#fromdate').click(function(){
$('#yosh').attr('disabled',true);
$('#fromdate').removeAttr('disabled');
});
$('#yosh').click(function(){
$('#yosh').removeAttr('disabled');
$('#fromdate').attr('disabled',true);
});
});
</script>
回答by Joko Wandiro
回答by Warren Stevens
$('#fromdate').disabled = false
$('#fromdate').disabled = false