尝试根据用户在选择中的选择来显示/隐藏文本文件。我相信我的问题出在 if 语句上。
<!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>
最佳答案
简单的语法错误,在 if
后面有一个额外的 {
。删除它就可以了。
js
$("#drpStatus").change(function(){
if( $(this).val() == 'Married') /*removed the {*/
$("#txtSpouse").show();
else
$("#txtSpouse").hide();
});
关于javascript - JQuery:onchange 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15030526/