日期:2014-05-16  浏览次数:20769 次

怎样用jquery让texbox根据条件隐藏或者显示
现在有下拉列表的两种类别
1.服务
2.百货

当我添加商品的时候
如果在下拉列表中 选择1 就会多出一个txtbox 显示商品有效期
如果在下拉列表中 选择2 这个txtbox就不会显示出来



------解决方案--------------------

使用jquery实现,运行时需要保证在同级目录存在jquery的核心js文件"jquery-1.4.2.js"
<html>
  <head>
    <title>select.html</title>

<script src="jquery-1.4.2.js"></script>
<script language="javascript">
$().ready(function(){
   $("select[name='buy']").unbind("change");
   $("select[name='buy']").bind("change",function(){
if($(this).val()=="service") {
$("input[name='protecttime']").show();
} else {
$("input[name='protecttime']").hide();
}
   });
});
</script>

  </head>
  
  <body>
    
<select name="buy">
<option value="default">-请选择-</option>
<option value="service">服务</option>
<option value="thing">百货</option>
</select>

<input type="text" name="protecttime" value="180天" style="display:none" />

  </body>
</html>