2012-04-12 38 views
1

我的一个网页上隐藏了某些表单的某些部分与某些用户无关的代码。但是,由于某种原因,现在只有selectAttanding变量被绑定。我想传递一个文本框和两个下拉列表的值,但只传递此下拉列表的值。有什么建议么?JQuery仅查看一个DIV ID以获取值

<html>  
<input class="input-xlarge focused" id="txtName" type="text" > 
<select name=selectAttending id=selectAttending> 
    <option>Select One...</option> 
    <option>Yes</option> 
    <option>No</option> 
</select> 
<select id=selectGender> 
    <option selected="selected">Select One...</option> 
    <option>Male</option> 
    <option>Female</option> 
</select> 

<script language=javascript> 
$("#selectAttending").change(function() { 
    if ($('#selectAttending option:selected').text() == "No") { 
     $('#ifAttending').hide(); 
    } else if ($('#selectAttending option:selected').text() == "Yes") { 
     $('#ifAttending').show(); 
    } else { 
     $('#ifAttending').show(); 
    } 
});  
</script> 
+3

你有多个具有相同ID的HTML元素吗?如果是这样,你需要将它们改为类。 ID本质上应该是唯一的,所以如果你有多个具有相同ID的元素,那么JavaScript将只使用第一个。 – 2012-04-12 18:18:14

+1

请发布您的HTML。 – j08691 2012-04-12 18:22:29

+0

很明显,必须为每个元素设置name属性以使其正常工作以及ID。我只设置了ID。 – Geekender 2012-04-12 18:30:46

回答

0

jQuery选择创建元素列表;当您使用jQuery选择功能时,例如$,如果要处理多个结果,则需要遍历结果。尝试使用像each这样的功能。请参阅this S/O question和相关的jQuery docs