简体   繁体   English

JavaScript取消隐藏文字元素无效

[英]Javascript unhide text element not working

I have searched around the internet but I still can't find a solution to my problem. 我已经在互联网上进行搜索,但是仍然找不到解决我问题的方法。 The issue I have is that I have a HTML selectbox and a text box as follows: 我的问题是我有一个HTML选择框和一个文本框,如下所示:

 <p>
    <label for="car_parking">Issue a car park pass ?:</label>
    <select name="car_park_pass" id="car_park_pass" tabindex="<?php echo $tab++; ?>">
       <option value="Please select">Please select</option>
       <option value="yes">Yes</option>
       <option value="no">No</option>   
    </select>
 </p>
 <div id="reg_number">  
    <p>
       <label for="car_reg_no">Car registration number</label>
       <input type="text" name="car_reg_no" id="car_reg_no" tabindex="<?php echo $tab++; ?>">
    </p>
 </div>

The problem I have is that I want to display "reg_number" if "car_park_pass" is equal to "yes" but have it be hidden by default. 我的问题是,如果“ car_park_pass”等于“ yes”,我想显示“ reg_number”,但默认情况下将其隐藏。 I have the following javascript which I tried putting in the head and the body but nothing seemed to work: 我尝试将以下javascript放在头部和身体中,但似乎无济于事:

 $('select[name=car_park_pass]').change(function() 
 {
      if ($(this).val() == 'yes') 
      {
           $('#reg_number').show();
      } 
      else 
      {
           $('#reg_number').hide();
      }
 });

What do I need to do to make this work? 我需要做些什么才能使这项工作?

Try this: 尝试这个:

$('select[name=car_park_pass]').change(function() {
  if ($(this).val() == 'yes') 
  {
      $('#reg_number').show();
  } 
  else 
  {
      $('#reg_number').hide();
  }
});

Here is the html 这是HTML

<p><label for="car_parking">Issue a car park pass ?:</label>
 <select name="car_park_pass" id="car_park_pass" tabindex="<?php echo $tab++; ?>">
 <option value="Please select">Please select</option>
 <option value="yes">Yes</option>
 <option value="no">No</option>   
 </select></p>
 <div id="reg_number" style="display:none;">  
 <p><label for="car_reg_no">Car registration number</label>
 <input type="text" name="car_reg_no" id="car_reg_no" tabindex="<?php echo $tab++; ?>"></p>
 </div>

and here is the javascript 这是javascript

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

      if ($(this).val() == 'yes') 
           {
                $('#reg_number').show();
           } 
           else 
           {
                $('#reg_number').hide();
           }

});

You can see it working here http://jsfiddle.net/utn74a0s/ 您可以在这里查看它的运行情况http://jsfiddle.net/utn74a0s/

this code works, the problem was using .on() 此代码有效,问题出在使用.on()

this jquery code : 这个jQuery代码:

$(document).ready(function(){
    $( "#car_park_pass" ).on( "change", function() {
        if ($(this).val() == 'yes'){
            $('#reg_number').show();
        } 
        else{
            $('#reg_number').hide();
        }
    });
});
  • You forgot the $ symbol in this code ('select[name=car_park_pass]') . 您在此代码中忘记了$符号('select[name=car_park_pass]')
  • In the last line, }); 在最后一行, }); , the brace is not open ,撑杆未打开
  • Call the change function when the document is ready, it's more efficient 准备好文档后调用change函数,效率更高
  • Select by ID si more efficient $('#car_park_pass') 通过ID si选择更有效的$('#car_park_pass')

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <label for="car_parking">Issue a car park pass ?:</label> <select name="car_park_pass" id="car_park_pass"> <option value="Please select">Please select</option> <option value="yes">Yes</option> <option value="no">No</option> </select> </p> <div id="reg_number"> <p> <label for="car_reg_no">Car registration number</label> <input type="text" name="car_reg_no" id="car_reg_no"> </p> </div> <script type="text/javascript"> $(document).ready(function(){ $('#car_park_pass').on('change', function() { if ($(this).val() == 'yes'){ $('#reg_number').show(); } else{ $('#reg_number').hide(); } }); }); </script> 

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

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