简体   繁体   English

当从下拉列表中选择一个值时,jQuery将输入文本变为只读

[英]jQuery make an input text into read only when one value is selected from drop down list

I have my html markup is something like this 我有我的html标记是这样的

<table id="sales-product-details">
  <tbody id="tableToModify">
    <tr>
        <td>
          <select id="prd_dtype1" class="discount-type" name="prddtype[]">
            <option value="">none</option>
            <option value="1">discount type 1</option>
            <option value="2">discount type 2</option>
            <option value="3">discount type 3</option>
          </select>
        </td>
        <td>
          <input type="text" class="dicount-price" onkeyup="calprice2(this.value,'1');" id="prd_damount1" name="prddamount[]">
        </td>
      </tr>
    </tbody>
  </table>

Now here I want in jQuery that when I will select none from the dropdown list of discount type then the input text field for discount price will be read only and when I will select any value other then none then the input text field for discount price will be turn into input area( no read only ). 现在在这里我想在jQuery中,当我从折扣类型的下拉列表中选择一个都没有时,则折扣价的输入文本字段将是只读的;而当我选择其他值时,则不输入任何价格,则折扣价的输入文本字段将被读取变成输入区域(非只读)。 Any help and suggestions will be really appreciable. 任何帮助和建议都是非常可取的。 Thanks.. 谢谢..

use select change() event to get the selected value and prop() to change the readonly property of input 使用select change()事件获取所选值,并使用prop()更改输入的readonly属性

 $('#prd_dtype1').change(function(){
    if(this.value == ""){
        $('#prd_damount1').prop('readonly',true);
     } else{
      $('#prd_damount1').prop('readonly',false);
   }
 }).change(); //<--- calling the change event here to make sure change event  is called as soon as the document is ready ...

OR The easy way (without the conditions) 或简单方法(无条件)

 $('#prd_dtype1').change(function(){
    $('#prd_damount1').prop('readonly',!this.value);
 }).change();

and just incase if you need the input value to be empty when none is selected then you can use 只是以防万一,如果在未选择任何一个的情况下您需要输入值为空,则可以使用

 $('#prd_damount1').val('');

inside the if condition of first answer. 在第一个答案的if条件中。

fiddle here 在这里摆弄

Try 尝试

$('#sales-product-details').on('change', '.discount-type', function(){
    var $this = $(this), val = $this.val();
    $this.closest('tr').find('.dicount-price').prop('readOnly', !val)
})

Demo: Fiddle 演示: 小提琴

$("#prd_dtype1").change(function(){

   if($(this).val()==""){
      $(".dicount-price").attr("readonly",true);

   }else{
       $(".dicount-price").removeAttr("readonly");
}
});
$("#prd_dtype1").change(function(){

   if( $(this).val() == '' ){
      $("#prd_damount1").attr("readonly",false);
   }else{
       $("#prd_damount1").attr("readonly",true);
   }
});

Check Js Fiddle http://jsfiddle.net/rGMKK/3/ 检查Js Fiddle http://jsfiddle.net/rGMKK/3/

$(function () { $(函数(){

      $("#prd_damount1").attr("disabled", "disabled");


      $("#prd_dtype1").on("change", function () {

          console.log($(this).val());
          if ($(this).val() === "") {

              $("#prd_damount1").attr("disabled", "disabled");


          } else {

              $("#prd_damount1").removeAttr("disabled");


          }

      });


  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM