Javascript jQuery复选框更改并单击事件
Javascript jQuery复选框更改并单击事件,javascript,jquery,checkbox,event-handling,Javascript,Jquery,Checkbox,Event Handling,$(文档).ready(函数(){ //设置初始状态。 $('#textbox1').val($(this).is(':checked'); $('#checkbox1')。更改(函数(){ $('#textbox1').val($(this).is(':checked'); }); $('#复选框1')。单击(函数(){ 如果(!$(this).is(':checked')){ 返回确认(“您确定吗?”); } }); }); 使用mousedown $('#checkbox1').mo
$(文档).ready(函数(){
//设置初始状态。
$('#textbox1').val($(this).is(':checked');
$('#checkbox1')。更改(函数(){
$('#textbox1').val($(this).is(':checked');
});
$('#复选框1')。单击(函数(){
如果(!$(this).is(':checked')){
返回确认(“您确定吗?”);
}
});
});代码>
使用mousedown
$('#checkbox1').mousedown(function() {
if (!$(this).is(':checked')) {
this.checked = confirm("Are you sure?");
$(this).trigger("change");
}
});
去掉更改事件,改为更改单击事件中文本框的值。与其返回确认的结果,不如在变量中捕捉它。如果为true,则更改该值。然后很好地返回变量。。为了避免头痛(这里已经过了午夜),我可以想出:
$('#checkbox1').click(function() {
if (!$(this).is(':checked')) {
var ans = confirm("Are you sure?");
$('#textbox1').val(ans);
}
});
希望它有帮助试试这个
$('#checkbox1').click(function() {
if (!this.checked) {
var sure = confirm("Are you sure?");
this.checked = sure;
$('#textbox1').val(sure.toString());
}
});
复选框单击并检查同一事件循环中的值是问题所在
试试这个:
$('#checkbox1').click(function() {
var self = this;
setTimeout(function() {
if (!self.checked) {
var ans = confirm("Are you sure?");
self.checked = ans;
$('#textbox1').val(ans.toString());
}
}, 0);
});
let checkbox = document.getElementById('checkboxId');
if (checkbox.checked != true)
{
alert("select checkbox");
}
演示:在中进行了测试,并完成了您的要求。这种方法还有一个额外的好处,即在单击与复选框关联的标签时触发
更新答案:
$(document).ready(function() {
//set initial state.
$('#textbox1').val(this.checked);
$('#checkbox1').change(function() {
if(this.checked) {
var returnVal = confirm("Are you sure?");
$(this).prop("checked", returnVal);
}
$('#textbox1').val(this.checked);
});
});
$(document).ready(function() {
//set initial state.
$('#textbox1').val($(this).is(':checked'));
$('#checkbox1').change(function() {
if($(this).is(":checked")) {
var returnVal = confirm("Are you sure?");
$(this).attr("checked", returnVal);
}
$('#textbox1').val($(this).is(':checked'));
});
});
原始答案:
$(document).ready(function() {
//set initial state.
$('#textbox1').val(this.checked);
$('#checkbox1').change(function() {
if(this.checked) {
var returnVal = confirm("Are you sure?");
$(this).prop("checked", returnVal);
}
$('#textbox1').val(this.checked);
});
});
$(document).ready(function() {
//set initial state.
$('#textbox1').val($(this).is(':checked'));
$('#checkbox1').change(function() {
if($(this).is(":checked")) {
var returnVal = confirm("Are you sure?");
$(this).attr("checked", returnVal);
}
$('#textbox1').val($(this).is(':checked'));
});
});
如果您使用cb name
,大多数答案(大概)都听不懂。这意味着当您单击标签时,它将选中该复选框,而不是直接单击复选框。(不完全是这个问题,但各种搜索结果都会出现在这里)
jQuery 1.7+
$('#checkbox1').on('change', function() {
// From the other examples
if (!this.checked) {
var sure = confirm("Are you sure?");
this.checked = !sure;
$('#textbox1').val(sure.toString());
}
});
- 添加了JSFIDLE示例和带有可点击复选框标签的html
只需使用单击事件
我的复选框id是CheckAll
$('#CheckAll').click(function () {
if ($('#CheckAll').is(':checked') == true) {
alert(";)");
}
});
对我来说,这非常有效:
$('#checkboxID').click(function () {
if ($(this).attr('checked')) {
alert('is checked');
} else {
alert('is not checked');
}
})
给你
Html
<input id="ProductId_a183060c-1030-4037-ae57-0015be92da0e" type="checkbox" value="true">
JavaScript
<script>
$(document).ready(function () {
$('input[id^="ProductId_"]').click(function () {
if ($(this).prop('checked')) {
// do what you need here
alert("Checked");
}
else {
// do what you need here
alert("Unchecked");
}
});
});
</script>
$(文档).ready(函数(){
$('input[id^=“ProductId”]')。单击(函数(){
if($(this.prop('checked')){
//在这里做你需要的
警报(“已检查”);
}
否则{
//在这里做你需要的
警惕(“未检查”);
}
});
});
$('#复选框1')。单击(函数(){
如果($(this).is(“:checked”)){
var returnVal=确认(“您确定吗?”);
$(this.attr(“checked”,returnVal);
}
$('#textbox1').val($(this).is(':checked');
});
通过名称获取无线电值
$('input').on('className', function(event){
console.log($(this).attr('name'));
if($(this).attr('name') == "worker")
{
resetAll();
}
});
我不知道为什么每个人都把事情弄得这么复杂。
这就是我所做的一切
if(!$(this).is(":checked")){ console.log("on"); }
如果您使用的是iCheck Jquery,请使用以下代码
$("#CheckBoxId").on('ifChanged', function () {
alert($(this).val());
});
延迟回答,但您也可以在(“更改”)上使用on
$('#check')。在('change',function()上{
var checked=this.checked
$('span').html(选中.toString())
});代码>
检查我代码>试试看
checkbox1.onclick=e=>{
如果(!checkbox1.checked)checkbox1.checked=!确认(“您确定吗?”);
textbox1.value=checkbox1.checked;
}
试试这个:
$('#checkbox1').click(function() {
var self = this;
setTimeout(function() {
if (!self.checked) {
var ans = confirm("Are you sure?");
self.checked = ans;
$('#textbox1').val(ans.toString());
}
}, 0);
});
let checkbox = document.getElementById('checkboxId');
if (checkbox.checked != true)
{
alert("select checkbox");
}
它在检查时显示警报,并且总是阻止我实际检查。这在Chrome上。与@pimvdm相同。即使是选中操作也会弹出确认窗口(它只会在取消选中时弹出),选择“确定”不会导致选中该框。使用鼠标时,确认窗口会起作用,但如果使用键盘进行检查,则确认窗口不会起作用。@frinux这很简单,因为这是一个与鼠标相关的问题。请不要基于答案与完全不同的问题的相关性而对其进行否决。如果您在通过键盘检查复选框后触发指示灯状态时遇到问题,请发布一个有关该问题的问题,而不是漫不经心地对答案进行向下投票。向下投票,因为鼠标向下不是用户与复选框交互的唯一方式。它在FF和chrome中工作,并在IE 8中解释了其行为。因此,请注意您需要在哪些浏览器中使用此功能,以及在哪些浏览器中看到此错误,这可能很重要。这不是最好的,但我相信这对我来说是有效的:。通过web搜索发现了这一点。你应该使用'prop'而不是'attr'>>只是一个注释,使用这个要快得多。checked
而不是$(这个)。is(':checked')
:@Dakota同意,速度要慢得多,但我们仍然在谈论每秒60万次的操作。每毫秒600次。我认为,如果这开始导致网页出现性能问题,您可能需要重新评估javascript;)不过,了解代码的性能指标是很好的。谢谢。@MikeU:同意你的过早优化,但是考虑到this.checked
比编写+本机javascript要短得多,一般来说它可能真的值得使用(除了速度快约200到300倍)。我建议使用.prop()而不是.attr()由于后者并非在所有情况下都有效,我相信jQuery现在推荐.prop(),我认为,[这]在$('#textbox1').val(this.checked)中;指文件。它应该是$('#textbox1').val($('#checkbox1').is(':checked'));它是#OuterDivOrBody不是。OuterDivOrBody:)我知道这是非常古老的,但我在寻找其他东西时遇到它,想让你知道,在第二个示例中(我没有检查第一个提琴),如果该人单击“取消”,你仍然取消选中并显示为false,如果他们取消,那么它应该保留选中并显示为true。以防您想要修复它。纯代码的答案被认为是低质量的:请确保提供一个解释,说明您的代码是做什么的,以及它是如何解决问题的。如果你能在你的文章中添加更多的信息,这将有助于询问者和未来的读者。另请参见解释完全基于代码的答案:谢谢,上面的答案非常详细,您的答案非常简单。谢谢
$("#person_IsCurrentAddressSame").change(function ()
{
debugger
if ($("#person_IsCurrentAddressSame").checked) {
debugger
}
else {
}
})
let checkbox = document.getElementById('checkboxId');
if (checkbox.checked != true)
{
alert("select checkbox");
}