admin管理员组文章数量:1332872
I have an input type select
and input type text
. What I want is that when I select on "Others" item in the select input, the other input type text will enable, if not it will be disabled. I have a code here, I probably miss something here.
<script type='text/javascript'>
var others = $('select[name="facility"]').val()
$().ready(function() {
if(others == "Others"){
$('input').each(function() {
if ($('#disabled_input').attr('disabled')) {
$('#disabled_input').removeAttr('disabled');
}
else {
$('#disabled_input').attr({
'disabled': 'disabled'
});
}
});
}
});
});
</script>
I have an input type select
and input type text
. What I want is that when I select on "Others" item in the select input, the other input type text will enable, if not it will be disabled. I have a code here, I probably miss something here.
<script type='text/javascript'>
var others = $('select[name="facility"]').val()
$().ready(function() {
if(others == "Others"){
$('input').each(function() {
if ($('#disabled_input').attr('disabled')) {
$('#disabled_input').removeAttr('disabled');
}
else {
$('#disabled_input').attr({
'disabled': 'disabled'
});
}
});
}
});
});
</script>
Share
Improve this question
asked Oct 10, 2013 at 5:50
IkongIkong
2,5704 gold badges40 silver badges61 bronze badges
2 Answers
Reset to default 6Use onchange
event to get the value of select and i assume disabled_input
id is assigned to only one input field then there is no sense to loop through all the inputs
<script type='text/javascript'>
$(document).ready(function() {
$('#disabled_input').attr('disabled','disabled');
$('select[name="facility"]').on('change',function(){
var others = $(this).val();
if(others == "Others"){
$('#disabled_input').removeAttr('disabled');
}else{
$('#disabled_input').attr('disabled','disabled');
}
});
});
</script>
See demo here
I don't think there is any need of each
here.
For every iteration it will check if ($('#disabled_input').attr('disabled'))
which u can directly check
var others = $('select[name="facility"]').val();
$(document).ready(function () {//missing document
if (others == "Others") {
//no need of each here
if ($('#disabled_input').attr('disabled')) {
$('#disabled_input').removeAttr('disabled');
}
else {
$('#disabled_input').attr({'disabled': 'disabled'});
}
}
});
本文标签: javascriptJQuery enabledisable input when right select value selectedStack Overflow
版权声明:本文标题:javascript - JQuery enabledisable input when right select value selected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742308486a2450394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论