2015-11-16 50 views
0

jQuery出现值问题。 如何在选择选项值other时获取输入字段?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; 
      } 
     }); 

    }); 

+0

欢迎SO。请查看[此链接](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work) – mplungjan

回答

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" />

1

这样的 - 请注意我删除了前导空格的值和处理重载。

$(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" />

相关问题