【发布时间】:2019-07-28 04:43:07
【问题描述】:
当用户选中一个复选框时,我想执行一个操作,但我无法让它工作,我做错了什么?
所以基本上,用户访问我的页面,勾选框,然后弹出警报。
if($("#home").is(":checked"))
{
alert('');
}
【问题讨论】:
当用户选中一个复选框时,我想执行一个操作,但我无法让它工作,我做错了什么?
所以基本上,用户访问我的页面,勾选框,然后弹出警报。
if($("#home").is(":checked"))
{
alert('');
}
【问题讨论】:
您要查找的内容称为事件。 JQuery 提供了类似这样的简单事件绑定方法
$("#home").click(function() {
// this function will get executed every time the #home element is clicked (or tab-spacebar changed)
if($(this).is(":checked")) // "this" refers to the element that fired the event
{
alert('home is checked');
}
});
【讨论】:
实际上,change() 函数更适合此解决方案,因为它适用于 javascript 生成的操作,例如通过脚本选择每个复选框。
$('#home').change(function() {
if ($(this).is(':checked')) {
...
} else {
...
}
});
【讨论】:
您需要使用此处描述的 .click 事件:http://docs.jquery.com/Events/click#fn
所以
$("#home").click( function () {
if($("#home").is(":checked"))
{
alert('');
}
});
【讨论】:
$("#home").click(function() {
var checked=this.checked;
if(checked==true)
{
// Stuff here
}
else
{
//stuff here
}
});
【讨论】:
$( "#home" ).change(function() {
if(this.checked){
alert("The Check-box is Checked"); // Your Code...
}else{
alert("The Check-box is Un-Checked"); // Your Code...
}
});
【讨论】:
在某些情况下,当您拥有动态内容时,您可以使用以下代码:
$(function() {
$(document).on('click','#home',function (e) {
if($(this).is(":checked")){
alert('Home is checked')
}else{
alert('Home is unchecked')
}
});
});
【讨论】: