简体   繁体   English

使用jQuery从下拉列表中选择一个值时,将输入文本设置为只读

[英]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, 我那里有一个选择框,

  • none 没有
  • current 当前
  • future 未来

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.

相关问题 使用jQuery从下拉列表中获取选定的文本 - Get selected text from a drop-down list using jQuery 从下拉列表中获取所选文本,但使用jQuery删除数值 - Get selected text from a drop-down list but remove numeric value using jQuery 如何使HTML表单的下拉列表中的所选选项之一添加文本输入? - How to make one of the selected options in a drop-down list of an HTML form add a text input? 使用jQuery /纯jscript从HTML下拉列表(选择下拉列表时下拉列表)中获取事件数据 - Get event data from an HTML drop down list (the list that drops down when a drop down list is selected) using jQuery / plain jscript 当在一个下拉列表中选择一个选项时,如何使用javascript在另一个下拉列表中显示其相关值 - when one option selected in one drop down list then how to display its related value in another drop down list using javascript 必须使用jquery验证插件从多个下拉列表中选择至少一个下拉列表值 - Atleast one drop down value must be selected from multiple drop downs using jquery validation plugin 如何从html下拉列表菜单中仅打印一个选项的选定值 - How to print the selected value of only one option from html drop down list menu 选择下拉菜单中的选项时,将显示输入类型文本 - When an option from drop down is selected input type text appears 读取所有下拉列表的选定值 - Read selected value of all the drop down list 选择下拉菜单中的列表时,使div消失 - Make div disappear when list from drop down menu is selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM