如何在复选框更改单击时唯一标识 jQuery 函数
How to uniquely identify the jQuery function on checkBox change click?
在我的程序中,我一次显示一个问题,下次单击它是发布并得到第二个问题。但是我为 Jquery 中的每个问题的复选框列表应用了一些逻辑,我像这样称呼它
$(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);
}
}
});
});
所以问题是,对于一个问题,逻辑与另一个复选框列表问题不同,我需要调用适当的函数,如你上面在 document.ready 中看到的那样有两个函数。那么对于某些特定问题,我应该如何修复函数调用呢?
在每个
帖子中使用jQuery取消绑定函数来删除事件处理程序绑定。作为参考,请看一下。
您可以使用一些自定义事件并根据问题触发适当的事件,这里有一些代码:
$(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");
}
});
});
希望对您有所帮助
相关文章:
- 使用onkeyup JS事件检查输入的值是否唯一
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- JavaScript-用唯一属性标识对象
- 挖空映射 - 使用键唯一标识嵌套对象
- 唯一标识父类的元素
- 唯一标识重复的Chrome选项卡
- 唯一标识文件而无需下载
- D3.js如何在组条形图中唯一标识单个条形
- 如何在复选框更改单击时唯一标识 jQuery 函数
- 唯一的设备标识
- 网站的唯一用户标识
- 有没有一种方法可以为我的Chrome扩展唯一标识内容脚本运行的iframe
- 我应该使用Javascript或PHP设置一个全局变量来唯一标识web应用程序中的每个页面吗
- 在JavaScript中生成一个(非udid)标识符来唯一标识iOS设备
- 如何在没有唯一标识属性但有HTML注释的情况下使用jQuery选择HTML
- 唯一标识Flash内容
- 用node.js在POST请求中唯一标识用户
- 在使用HTML5文件API上传之前唯一标识文件
- 唯一标识向Web API发出请求的客户端的内容
- 这是唯一标识从Ajax到Servlet请求的客户机状态的最佳选择