다음 코드가 있습니다.
$(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
q60
내 jQuery 코드를 살펴 봤는데 내가 어디로 잘못 가고 있는지 알 수 없습니다. 아니면 이것이 이상한가요?
이벤트 바인딩에 문제가 있습니다.의 경우 AfterSales
클릭 이벤트를 q59에 바인딩합니다. 따라서 사용자 Sales
가 q59에서 무언가를 선택 하고 선택하면 클릭 핸들러가 실행됩니다. 다음은 문제를 일으키는 코드입니다.
$("[name=q59]").each(function (i) {
$(this).click(function () { ...
You can avoid this by either unbinding the event or using some condition inside the event to check whether AfterSales
is selected or Sales
is selected.
To unbind use
$("[name=q59]").each(function (i) {
$(this).unbind('click');
});
in else part.
See working fiddle
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다