嗨我有一个复选框,当我点击它并选中一个函数运行的盒子,这就是我想要它的方式..现在我想运行一个不同的功能,如果它被检查但它只是运行每次都有相同的功能。
<input type="checkbox" class="no-custom" onclick="CheckBox()">
function CheckBox() {
$("#emailMain").css({"display": "none"});
$("#emailSame").css({"display": "inline"});
var Mainemail = Customer().email['#text']();
Contact().email = Mainemail;
EmailHolder(Mainemail);
}
有哪些想法以最佳方式对此进行排序?
答案 0 :(得分:3)
首先,如果您在网页中添加了jQuery,则应使用它来附加您的活动,因为它可以更好地分离关注点。然后,您可以使用元素的checked
属性来确定要调用的函数:
<input type="checkbox" class="no-custom" />
$('.no-custom').change(function() {
if (this.checked) {
// do something...
}
else {
// do something else...
}
});
答案 1 :(得分:0)
在复选框中添加ID:
<input type="checkbox" id="the-checkbox" class="no-custom" onclick="CheckBox()">
然后为其更改时添加事件侦听器:
$(function() {
$('#the-checkbox').change(function() {
if($(this).is(':checked')) {
oneFunction();
}
else {
anotherFunction();
}
});
function oneFunction() {
}
function anotherFunction() {
}
});
答案 2 :(得分:0)
您可以使用jQuery is轻松检查复选框是否已选中。
<input type="checkbox" class="no-custom" onclick="CheckBox(this)">
function CheckBox(cb) {
if ($(cb).is(":checked")) {
alert("Checked");
CheckedFunction();
} else {
alert("Unchecked");
UncheckedFunction
}
}