dongrong5189
2016-09-16 06:41
浏览 35
已采纳

选择选项后禁用输入字段

_this is my UI

<script>
    function changeTextbox()
    {
        $("#service_request").on("change", function(){
        if ($(this).val() == "Group Code")
        {
        $("#employee_id").attr("disabled","disabled");
        } else {
         $("#employee_id").removeAttr("disabled");
       }

  });
}

_that is my script

<select style="width:199px;border-radius:10px;" id="service_request" name="service_request" class="form-control" onchange="showUser(this.value)">

    <option value="Default Request" selected disabled>Default Request Service</option>
        <?php if(isset($records)): foreach($records as $row):?>
                <option value="<?php echo $row->service_group;?>"><?php echo $row->service_group;?></option>                                
            <?php endforeach;?>                     
            <?php else:?>
                <p>Request Service not available</p>
            <?php endif;?>

_this is how my select options goes

 <input style="border-radius:10px;width:425px;height:35px;" type="text" id="employee_id"  name="assign_to" onkeyup="autocomplet()" placeholder="Approver" onchange="changeTextbox()"/>

_this is for my input text field

enter image description here _its the value in my inspect element

** _I don't know what's going on with this code, the Approver text field should be disabled, if the Group Code service is selected, but it turn's out to be not working, someone help me please :D**

图片转代码服务由CSDN问答提供 功能建议

_这是我的用户界面< / p>

 &lt; script&gt; 
 function changeTextbox()
 {
 $(“#service_request”)。on(“change”,function(){
 if(  $(this).val()==“Group Code”)
 {
 $(“#employee_id”)。attr(“disabled”,“disabled”); 
} else {
 $(“#  employee_id“)。removeAttr(”disabled“); 
} 
 
}); 
} 
   
 
 

_这是我的脚本

 &lt; select style =“width:199px; border-radius:10px;”  id =“service_request”name =“service_request”class =“form-control”onchange =“showUser(this.value)”&gt; 
 
&lt; option value =“默认请求”已选中已禁用&gt;默认请求服务&lt; / 选项&gt; 
&lt;?php if(isset($ records)):foreach($ records as $ row):?&gt; 
&lt; option value =“&lt;?php echo $ row-&gt; service_group;?  &gt;“&gt;&lt;?php echo $ row-&gt; service_group;?&gt;&lt; / option&gt;  
&lt;?php endforeach;?&gt;  
&lt;?php else:?&gt; 
&lt; p&gt;请求服务不可用&lt; / p&gt; 
&lt;?php endif;?&gt; 
   
 
  

_这就是我的选择选项的方式

 &lt; input style =“border-radius:10px; width:425px  ;高度:35px;”  type =“text”id =“employee_id”name =“assign_to”onkeyup =“autocomplet()”placeholder =“Approver”onchange =“changeTextbox()”/&gt; 
   
 
  n 

_这是我输入的文字字段

检查我的检查元素中的值

** _我不知道这段代码发生了什么,如果选择了群组代码服务,应该禁用审批者文本字段,但是它会变得不起作用,请有人帮助我:D * *

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duannue2455 2016-09-16 06:59
    已采纳

    The textbox disabled property is dependent on select change event so onchange for textbox is useless so remove the function changeTextbox() . In your script on select change event do

    $(document).ready(function(){
       $('#service_request').on('change',function(){
    
        if($(this).val() == 'Group Code' )
            //$("#employee_id").attr("disabled", "disabled"); 
              $("#employee_id").prop("disabled", true); 
        else
            //$("#employee_id").removeAttr("disabled");
              $("#employee_id").prop("disabled", false); 
        });
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doulan8330 2016-09-16 06:59

    Try this code, when you change the select.

    $(document).on("change","#service_request", function(){
            if ($(this).val() == "Group Code")
            {
               $("#employee_id").attr("disabled","disabled");
            } 
            else 
            {
               $("#employee_id").removeAttr('disabled');
            }
    }
    

    You can also use prop()

    By using removeAttr(), you are completely removing the disabled attribute itself - while prop() is merely setting the property's underlying boolean value to false.

    $('#employee_id').prop("disabled", true);
    $('#employee_id').prop("disabled", false);
    
    评论
    解决 无用
    打赏 举报
  • dongshen3352 2016-09-16 07:00

    Just use the on change event listener the using the disabled attr

    let opt = document.querySelector("select");
    let input = document.querySelector("input");
    opt.addEventListener("change",function(){
    
        input.disabled = true;
    
    });
    

    http://jsbin.com/mugina/1/edit?html,js,output

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题