【问题标题】:jQuery dropdown selected option show input fieldjQuery下拉选择选项显示输入字段
【发布时间】:2015-11-16 15:05:45
【问题描述】:

jQuery 获取值问题。 选择其他选项值时如何获取输入字段?

 <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
 <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>


 <select  id="carm3" name="interest">
 <option value="">    Dogs</option>
 <option value="">    Cats</option>
 <option value="">    Rabbits</option>
  <option  value="    other">other</option>
  </select>
 <input type="text" name="other_interest" style="display:none" />

<script>
      jQuery(document).ready(function() {
          jQuery("#carm3").val(3).text(function() {
          if (jQuery.inArray(jQuery("other"))){ 
           jQuery('input[name=other_interest]').show();   
              return false;
          } 
        });

  });

【问题讨论】:

标签: jquery select input


【解决方案1】:

你在这里:http://jsfiddle.net/Lxen6qp1/

jQuery(document).ready(function() {
    jQuery("#carm3").change(function() {
        if (jQuery(this).val() === 'other'){ 
            jQuery('input[name=other_interest]').show();   
        } else {
            jQuery('input[name=other_interest]').hide(); 
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="carm3" name="interest">
    <option value="d">Dogs</option>
    <option value="c">Cats</option>
    <option value="r">Rabbits</option>
    <option value="other">other</option>
</select>
<input type="text" name="other_interest" style="display:none" />

【讨论】:

    【解决方案2】:

    像这样 - 请注意我删除了值中的前导空格并处理重新加载。

    $(function() {
      $("#carm3").on("change",function() {
        $('input[name="other_interest"]').toggle(this.value == "other");
      }).change(); // in case of reload
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <select id="carm3" name="interest">
      <option value="">Dogs</option>
      <option value="">Cats</option>
      <option value="">Rabbits</option>
      <option value="other">other</option>
    </select>
    <input type="text" name="other_interest" style="display:none" />

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 2014-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多