JQuery:onchange事件 [英] JQuery: onchange event

查看:112
本文介绍了JQuery:onchange事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图根据select中的用户选择来显示/隐藏文本文件。我相信我的问题在于if语句。

 <!DOCTYPE html> 
< html>
< head>
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js\">
< / script>

< script> ($(this).val()==')
$(document).ready(function(){
$(#drpStatus已婚'){
$(#txtSpouse)。show();
else
$(#txtSpouse)。hide();
});
});
< / script>

< / head>

< body>
< input type =textid =txtSpousestyle =display:none>
< select name =drpStatusid =drpStatus>
< option value =Singleselected =selected> Single< / option>
< option value =约会>约会< / option>
< option value =已婚>已婚< / option>
< / select>
< / body>

< / html>


解决方案

简单的语法错误,你有一个额外的 {> if 之后)。删除它,这工作。



演示: http:// jsfiddle .net / yNqfM /


  $ (#drpStatus)。change(function(){
if($(this).val()=='Married')/ *移除{* /
$(#txtSpouse ).show();
else
$(#txtSpouse)。hide();
});


Trying to show/hide a text file based on the users selection in a select. I believe my problem is with the if statement.

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>

<script>
$(document).ready(function(){
  $("#drpStatus").change(function(){
    if( $(this).val() == 'Married'){
      $("#txtSpouse").show();
    else
      $("#txtSpouse").hide();
  });
});
</script>

</head>

<body>
<input type="text" id="txtSpouse" style="display:none">
<select name="drpStatus" id="drpStatus">
            <option value="Single" selected="selected">Single</option>
            <option value="Dating">Dating</option>
            <option value="Married">Married</option>
          </select>
</body>

</html>

解决方案

Simple syntax error, you have an extra { after your if. Remove it and this works.

demo: http://jsfiddle.net/yNqfM/

js

$("#drpStatus").change(function(){
 if( $(this).val() == 'Married') /*removed the {*/
  $("#txtSpouse").show();
 else
  $("#txtSpouse").hide();
});

这篇关于JQuery:onchange事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆