javascript - Show a textbox on drop down select - Stack Overflow

I tried doing onchange and onclick event and I'm having trouble with both of them. When executing

I tried doing onchange and onclick event and I'm having trouble with both of them. When executing my code, Firebug tells me my showTextBox function is not defined.

When the value 1 is selected for the State dropdown, I need to display the <div> which holds a textbox. I'm moved the onclick attribute in the option tags as well as tried onchange attribute and still get showTextBox() is not defined in Firebug.

<select id="AddrType" onclick="showTextBox();"
        height="30px" style="width: 90px"> 
  <option value="10"></option>
  <option value="0">City</option>
  <option value="1">State</option>
  <option value="2">Zip</option>
</select>   

 <br />

 <div id="stateText" style="visibility: hidden">
   State <input type="text" id="STATE"/>
 </div>

Here is the function that called when either the onclick or onchange events are triggered:

function showTextBox() {
    console.log("in check Drop down");
    if ($('#AddrType').val() == 'State') {
        $('#stateText').style.visibility = 'visible';
    }
    console.log($('#AddrType').val());

Any help with this. I've Googled it, but I can't seem to find something that works. I can't use the document.getElementById() function.

I tried doing onchange and onclick event and I'm having trouble with both of them. When executing my code, Firebug tells me my showTextBox function is not defined.

When the value 1 is selected for the State dropdown, I need to display the <div> which holds a textbox. I'm moved the onclick attribute in the option tags as well as tried onchange attribute and still get showTextBox() is not defined in Firebug.

<select id="AddrType" onclick="showTextBox();"
        height="30px" style="width: 90px"> 
  <option value="10"></option>
  <option value="0">City</option>
  <option value="1">State</option>
  <option value="2">Zip</option>
</select>   

 <br />

 <div id="stateText" style="visibility: hidden">
   State <input type="text" id="STATE"/>
 </div>

Here is the function that called when either the onclick or onchange events are triggered:

function showTextBox() {
    console.log("in check Drop down");
    if ($('#AddrType').val() == 'State') {
        $('#stateText').style.visibility = 'visible';
    }
    console.log($('#AddrType').val());

Any help with this. I've Googled it, but I can't seem to find something that works. I can't use the document.getElementById() function.

Share Improve this question edited Mar 5, 2013 at 17:19 Jt2ouan asked Mar 5, 2013 at 17:12 Jt2ouanJt2ouan 1,9629 gold badges34 silver badges55 bronze badges 1
  • I presume in your code you have a closing } for showTextBox? – Phil Commented Mar 5, 2013 at 17:32
Add a ment  | 

2 Answers 2

Reset to default 0

try this:

<script type='text/javascript'>

  $(document).ready(function(){
       $('#AddrType').change(function(){
           if ($(this).val() == '1') {
               $('#stateText').css({'display':'block'});           
           }
        });
  });


</script>

and update your select to this:

   <select id="AddrType" height="30px" style="width: 90px"> 
     <option value="10"></option>
     <option value="0">City</option>
     <option value="1">State</option>
     <option value="2">Zip</option>
  </select>   

also, this works perfectly fine.

   <select id="AddrType"  onchange="showTextBox()" height="30px" style="width: 90px"> 
      <option value="10"></option>
      <option value="0">City</option>
      <option value="1">State</option>
      <option value="2">Zip</option>
  </select>

and where showTextBox() is:

   function showTextBox(){
        if ($('#AddrType').val() == '1') {
                   $('#stateText').css({'visibility':'visible'});
    }

Your val() is probably returning "1" instead of "State" you're checking against.

Not only on jQuery but also when you POST the form data to a server, the value of AddrType will be the value attribute of the chosen option element ("City / State / Zip" in your case is just the 'caption' of the value).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745190302a4615832.html

相关推荐

  • javascript - Show a textbox on drop down select - Stack Overflow

    I tried doing onchange and onclick event and I'm having trouble with both of them. When executing

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信