[英]make an input text into read only when one value is selected from drop down list using jquery
i have one select box there in that options are, 我那里有一个选择框,
if you select 'NONE' the all text box should be editable. 如果选择“无”,则所有文本框均应可编辑。 if you select 'current' the class="current"
those text box should be editable and remaining should be only readonly. 如果您选择“当前”,则class="current"
那些文本框应该是可编辑的,其余的应该是只读的。 similarly same like to future also. 同样也一样喜欢未来。 When i select future the future text boxes should be editable and remaining should be readonly 当我选择将来时,将来的文本框应该是可编辑的,其余的文本框应该是只读的
$('#cognizant').change(function(){ if(this.value == ""){ $('.current').prop('readonly',true); } else{ $('.current').prop('readonly',false); } }).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="curator"> <tbody id="tableToModify"> <tr> <td> <select id="cognizant" class="discount-type" > <option value="">none</option> <option value="1">current</option> <option value="2">future</option> </select> </td> <td> <input type="text" id="clientbi" class="current"/> <input type="text" id="cognizantbi" class="current"/> <input type="text" id="futurevsm" class="future" /> <input type="text" id="futuresymbols" class="future" /> </td> </tr> </tbody> </table>
Let's change your javascript code only. 让我们只更改您的JavaScript代码。
We need to use jQuery's change()
function ( documentation ) and then validate the selected option. 我们需要使用jQuery的change()
函数( 文档 ),然后验证所选的选项。
The final code: 最终代码:
$('#cognizant').change(function(){
var value = $(this).val();
if(value == ''){
// NONE
$('#curator input[type="text"]').removeAttr('readonly');
}
else if(value == '1'){
// CURRENT
$('#curator input[type="text"]').attr('readonly', true);
$('#curator .current').removeAttr('readonly');
}
else if(value == '2'){
// FUTURE
$('#curator input[type="text"]').attr('readonly', true);
$('#curator .future').removeAttr('readonly');
}
});
See it in action: https://jsfiddle.net/x5fdLqff/4/ 实际观看: https : //jsfiddle.net/x5fdLqff/4/
$('#cognizant').change(function(){
switch(this.value) {
case "":
$('.current').prop('readonly',false);
$('.future').prop('readonly',false);
break;
case "1":
$('.current').prop('readonly',false);
$('.future').prop('readonly',true);
break;
case "2":
$('.current').prop('readonly',true);
$('.future').prop('readonly',false);
break;
}
}).change();
<table id="curator">
<tbody id="tableToModify">
<tr>
<td>
<select id="cognizant" class="discount-type" >
<option value="">none</option>
<option value="current">current</option>
<option value="future">future</option>
</select>
</td>
<td>
<input type="text" id="clientbi" class="current myinputs"/>
<input type="text" id="cognizantbi" class="current myinputs"/>
<input type="text" id="futurevsm" class="future myinputs" />
<input type="text" id="futuresymbols" class="future myinputs" />
</td>
</tr>
</tbody>
</table>
<script>
$('#cognizant').change(function(){
var type = $("#cognizant").val();
$( ".myinputs" ).each(function( index ) {
$( this ).prop('readonly', false);
});
if(type != ""){
$("."+type).each(function( index ) {
$( this ).prop('readonly', true);
});
}
});
</script>
I have change option value in select box and made little change in javascript code. 我在选择框中具有更改选项的值,并且在javascript代码中几乎没有更改。
hope it works for you. 希望对你有效。
As an alternative approach, with a small change in the html. 作为一种替代方法,对html进行了少量更改。 You add a class name of 'none' to all the input fields, which is the same as option 1. 您将类名“ none”添加到所有输入字段,这与选项1相同。
<table id="curator">
<tbody id="tableToModify">
<tr>
<td>
<select id="cognizant" class="discount-type" >
<option value="">none</option>
<option value="1">current</option>
<option value="2">future</option>
</select>
</td>
<td>
<input type="text" id="clientbi" class="current none"/>
<input type="text" id="cognizantbi" class="current none"/>
<input type="text" id="futurevsm" class="future none" />
<input type="text" id="futuresymbols" class="future none" />
</td>
</tr>
</tbody>
</table>
Then the jQuery to be like this. 然后jQuery就是这样。 Comments explain approach. 评论说明方法。
$(function(){
$('#cognizant').on("change", function() {
//Set all text boxes to readonly
$("input[type='text']").prop("readonly", true);
//Select the option text, based on its value.
var text = $("#cognizant option[value='"+$(this).val()+"']").text();
//Set the property readonly to false on the selected class.
$("."+text).prop('readonly', false);
}).trigger("change");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.