我有以下代码:
$(document).ready(function(){
$("[name=q58]").each(function (i) {
$(this).click(function () {
var selection = $(this).val();
alert('Q58 Value = '+selection)
if (selection == '1') {
$("#region").show();
$("[name=q59]").each(function (i) {
$(this).click(function () {
var selection = $(this).val();
alert('Q59 Value = '+selection)
if (selection == '1') {
$("#location").show();
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#east, #north, #scotland, #southeast, #southwest").hide();
$("#central").show();
}
else if (selection == '2') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #north, #scotland, #southeast, #southwest").hide();
$("#east").show();
}
else if (selection == '3') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #scotland, #southeast, #southwest").hide();
$("#north").show();
}
else if (selection == '4') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #southeast, #southwest").hide();
$("#scotland").show();
}
else if (selection == '5') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southwest").hide();
$("#southeast").show();
}
else if (selection == '6') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southeast").hide();
$("#southwest").show();
}
else {
$("#location").hide();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southeast, #southwest").hide();
}
});
});
}
else if (selection == '10') {
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southeast, #southwest").hide();
$("#location").hide();
$("#region").show();
}
else {
$("input:radio[name^='q59']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#region").hide();
$("#location").hide();
$("#central").hide();
}
});
});
});
在JsFiddle中工作,或者说不是那么有效,例子:参见JsFiddle中的例子:http://jsfiddle.net/ExPUu/1/
发生以下奇怪的行为:
用户选择Aftersales
,然后他们会看到q59
并可以选择Central
或East
,然后查看q60
并选择其他选项。这很好。
如果用户选择Bus, Coach and Engines
他们没有获得任何其他选项 - 再次正常工作。
奇怪的行为是当用户选择Sales
时,他们应该能够选择Central
或East
- 如果他们点击Sales
并且没别的,这很好用。 但如果他们选择了Aftersales
,然后改变了主意并选择了Sales
,然后选择了Central
或East
,则显示{{ 1}}它不应该。
我一直在查看我的jQuery代码,看不出我出错的地方。或者这是一个奇怪的事情?
答案 0 :(得分:1)
问题在于事件绑定
如果是AfterSales
,则将点击事件绑定到q59。因此,当用户选择Sales
并从q59中选择某些内容时,将执行单击处理程序。这是导致问题的一段代码:
$("[name=q59]").each(function (i) {
$(this).click(function () { ...
您可以通过取消绑定事件或在事件中使用某些条件来检查是否已选择AfterSales
或选择Sales
来避免这种情况。
取消绑定使用
$("[name=q59]").each(function (i) {
$(this).unbind('click');
});
in else part。
见工作fiddle
答案 1 :(得分:0)
你的代码遇到了一些逻辑问题,我在下面尝试修复......
$(文件)。就绪(函数(){
$("[name=q58]").each(function (i) {
$(this).click(function () {
var selection = $(this).val();
alert('Q58 Value = '+selection)
if (selection == '1') {
$("#region").show();
$("[name=q59]").each(function (i) {
$(this).click(function () {
var selection = $(this).val();
alert('Q59 Value = '+selection)
if (selection == '1') {
if($("[name=q58]:checked").val() == 1){
$("#location").show();
}
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#east, #north, #scotland, #southeast, #southwest").hide();
if($("[name=q58]:checked").val() == 1){
$("#central").show();
}
}
else if (selection == '2') {
if($("[name=q58]:checked").val() == 1){
$("#location").show();
}
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #north, #scotland, #southeast, #southwest").hide();
if($("[name=q58]:checked").val() == 1){
$("#east").show();
}
}
else if (selection == '3') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #scotland, #southeast, #southwest").hide();
$("#north").show();
}
else if (selection == '4') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #southeast, #southwest").hide();
$("#scotland").show();
}
else if (selection == '5') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southwest").hide();
$("#southeast").show();
}
else if (selection == '6') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southeast").hide();
$("#southwest").show();
}
else {
$("#location").hide();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southeast, #southwest").hide();
}
});
});
}
else if (selection == '10') {
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southeast, #southwest").hide();
$("#location").hide();
$("#region").show();
}
else {
$("input:radio[name^='q59']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#region").hide();
$("#location").hide();
$("#central").hide();
}
});
});
});
58。 您在哪个区域工作: 售后服务的 公共汽车,长途汽车和发动机 销售的
<div class="row" style="display:none;" id="region">
<div class="col1">
59.
</div>
<div class="col2a width400">
In which Region do you work: </div>
<div class="direct2">
<table style="table2">
<tr id="trr1" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
<td class="td1"><input name="q59" value="1" id="q591" type="radio"/></td>
<td class="td3"><label for="q591"> <b>Central</b></label></td>
</tr><tr id="trr2" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
<td class="td1"><input name="q59" value="2" id="q592" type="radio"/></td>
<td class="td3"><label for="q592"> <b>East</b></label></td>
</tr> </table>
</div>
</div>
<div class="row" style="display:none;" id="location">
<div class="col1">
60.
</div>
<div class="col2a width400">
In which Location do you work: </div>
<div class="direct2" style="display:none;" id="central">
<table style="table2">
<tr id="trlc1" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
<td class="td1"><input name="central-q60" value="1" id="q601" type="radio"/></td>
<td class="td3"><label for="q601"> <b>Customer Operations</b></label></td>
</tr><tr id="trlc2" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
<td class="td1"><input name="central-q60" value="2" id="q602" type="radio"/></td>
<td class="td3"><label for="q602"> <b>Parts</b></label></td>
</tr><tr id="trlc3" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
</table>
</div>
<div class="direct2" style="display:none;" id="east">
<table style="table2">
<tr id="trle5" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
<td class="td1"><input name="east-q60" value="5" id="q605" type="radio"/></td>
<td class="td3"><label for="q605"> <b>Boston</b></label></td>
</tr><tr id="trle6" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
<td class="td1"><input name="east-q60" value="6" id="q606" type="radio"/></td>
<td class="td3"><label for="q606"> <b>Doncaster VMU</b></label></td>
</tr><tr id="trle7" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
</table>
</div>