Javascript 如何在复选框更改单击时唯一标识jQuery函数?
在我的程序中,我一次显示一个问题,下一次单击它是post并获取第二个问题。但我为Jquery中的每个问题应用了一些复选框列表逻辑,我这样称呼它Javascript 如何在复选框更改单击时唯一标识jQuery函数?,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,在我的程序中,我一次显示一个问题,下一次单击它是post并获取第二个问题。但我为Jquery中的每个问题应用了一些复选框列表逻辑,我这样称呼它 $(document).ready(function () { $("input:checkbox").change(function () { var txt = $(this).parent().children("label").text(); if ($(this).is(':
$(document).ready(function () {
$("input:checkbox").change(function () {
var txt = $(this).parent().children("label").text();
if ($(this).is(':checked')) {
if ($(this).parent().children("label").text() == 'Not Sure') {
$("input:checkbox").attr('checked', false);
$("input:checkbox").attr('disabled', true);
$(this).attr('checked', true);
$(this).attr('disabled', false);
}
}
else {
if ($(this).parent().children("label").text() == 'Not Sure') {
$("input:checkbox").attr('checked', true);
$("input:checkbox").attr('disabled', false);
$(this).attr('checked', false);
}
}
});
$("input:checkbox").change(function () {
var txt = $(this).parent().children("label").text();
if ($(this).is(':checked')) {
if (txt == 'Not Sure' || txt == 'We Are Large Business' || txt == 'None of these apply') {
$("input:checkbox").attr('checked', false);
$("input:checkbox").attr('disabled', true);
$(this).attr('checked', true);
$(this).attr('disabled', false);
}
}
else {
if (txt == 'Not Sure' || txt == 'We Are Large Business' || txt == 'None of these apply') {
$("input:checkbox").attr('checked', false);
$("input:checkbox").attr('disabled', false);
$(this).attr('checked', false);
}
}
});
});
所以问题是,对于1个问题,逻辑与另一个复选框列表问题不同,我需要调用适当的函数,正如您在上面的文档中所看到的。ready有两个函数。那么,对于某些特定的问题,我应该如何修复对函数的调用呢?在每次发布时,都使用jQuery unbind函数来删除事件处理程序绑定。
请参阅。您可以使用一些自定义事件并根据问题触发相应的事件,以下是一些代码:
$(document).ready(function () {
$("input:checkbox").on("someQuestions", function () {
var txt = $(this).parent().children("label").text();
if ($(this).is(':checked')) {
if ($(this).parent().children("label").text() == 'Not Sure') {
$("input:checkbox").attr('checked', false);
$("input:checkbox").attr('disabled', true);
$(this).attr('checked', true);
$(this).attr('disabled', false);
}
}
else {
if ($(this).parent().children("label").text() == 'Not Sure') {
$("input:checkbox").attr('checked', true);
$("input:checkbox").attr('disabled', false);
$(this).attr('checked', false);
}
}
});
$("input:checkbox").on("anotherQuestions", function () {
var txt = $(this).parent().children("label").text();
if ($(this).is(':checked')) {
if (txt == 'Not Sure' || txt == 'We Are Large Business' || txt == 'None of these apply') {
$("input:checkbox").attr('checked', false);
$("input:checkbox").attr('disabled', true);
$(this).attr('checked', true);
$(this).attr('disabled', false);
}
}
else {
if (txt == 'Not Sure' || txt == 'We Are Large Business' || txt == 'None of these apply') {
$("input:checkbox").attr('checked', false);
$("input:checkbox").attr('disabled', false);
$(this).attr('checked', false);
}
}
});
$("input:checkbox").change(function(){
// apply some logic to find out if is one question or another
if ( someQuestion ) {
$(this).trigger("someQuestions");
}
else {
$(this).trigger("anotherQuestions");
}
});
});
希望能有帮助