我有一个变化事件,工作得很好,但我需要让它递归。
所以我有一个函数,在改变时被触发,它将“改变”其他基于类选择器的下拉列表(注意“下拉列表”,可能有多个)。此代理更改不会触发该函数,因此会失败。我怎样才能让它工作呢?
Code
$(document).ready(function () {
var activeDropBox = null;
$("select.drop-box").change(function () {
var questionId = $(this).attr("questionId");
var selectedAnswer = $(this).val();
activeDropBox = this;
alert(this.questionId);
$.ajax(
{
type: "POST",
url: answerChangedActionUrl,
data: { questionId: questionId, selectedValue: selectedAnswer },
success: function (data) {
SetElementVisibility(data.ShowElement, questionId);
}, error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('XMLHttpRequest:' + XMLHttpRequest.responseText);
alert('textStatus:' + textStatus);
alert('errorThrown:' + errorThrown);
}
});
});
function SetElementVisibility(visible, questionId) {
// I would like each child to then trigger the change event...
$(".childOf" + questionId)[visible ? 'show' : 'hide']('slow');
// Suggested code
//$(".childOf" + questionId + " select").trigger("change");
if (!visible) {
$(".childOf" + questionId + " select").attr('selectedIndex', 0);
}
}
}
到目前为止,这些建议似乎是有效的,但是当change事件触发ajax post时,它现在似乎失败了。我将会尝试一下但那是另一个问题了。