Pro*_*aos 528 javascript jquery event-handling
我有
$(document).ready(function() {
//set initial state.
$('#textbox1').val($(this).is(':checked'));
$('#checkbox1').change(function() {
$('#textbox1').val($(this).is(':checked'));
});
$('#checkbox1').click(function() {
if (!$(this).is(':checked')) {
return confirm("Are you sure?");
}
});
});Run Code Online (Sandbox Code Playgroud)
和
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="checkbox1"/><br />
<input type="text" id="textbox1"/>Run Code Online (Sandbox Code Playgroud)
此处更改的事件使用复选框状态更新文本框值.我使用click事件确认取消选中时的操作.如果用户选择取消,则复选标记将恢复,但更改甚至会在确认之前触发,使事物处于不一致状态(选中复选框时文本框显示为false).如何处理取消并保持文本框值与检查状态一致?
kas*_*ega 844
好吧,我没有看到与我匹配的答案,所以我会发布这个.在JSFiddle中测试并执行您要求的操作.当单击与复选框关联的标签时,此方法具有触发的额外好处.
原答案:
$(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);
});
});
Run Code Online (Sandbox Code Playgroud)
更新答案:
$(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'));
});
});
Run Code Online (Sandbox Code Playgroud)
Jos*_*kle 88
使用 mousedown
$('#checkbox1').mousedown(function() {
if (!$(this).is(':checked')) {
this.checked = confirm("Are you sure?");
$(this).trigger("change");
}
});
Run Code Online (Sandbox Code Playgroud)
lko*_*lko 47
如果你使用的话,大多数答案都不会捕捉它(大概)<label for="cbId">cb name</label>.这意味着当您单击标签时,它将选中该框而不是直接单击该复选框.(不完全是问题,但各种搜索结果往往会出现在这里)
<div id="OuterDivOrBody">
<input type="checkbox" id="checkbox1" />
<label for="checkbox1">Checkbox label</label>
<br />
<br />
The confirm result:
<input type="text" id="textbox1" />
</div>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您可以使用:
Earlier versions of jQuery:
$('#OuterDivOrBody').delegate('#checkbox1', 'change', function () {
// From the other examples
if (!this.checked) {
var sure = confirm("Are you sure?");
this.checked = !sure;
$('#textbox1').val(sure.toString());
}
});
Run Code Online (Sandbox Code Playgroud)
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());
}
});
Run Code Online (Sandbox Code Playgroud)
zar*_*run 24
嗯..只是为了挽救头痛(过去的午夜),我可以想出:
$('#checkbox1').click(function() {
if (!$(this).is(':checked')) {
var ans = confirm("Are you sure?");
$('#textbox1').val(ans);
}
});
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你
chr*_*ris 10
对我来说这很有用:
$('#checkboxID').click(function () {
if ($(this).attr('checked')) {
alert('is checked');
} else {
alert('is not checked');
}
})
Run Code Online (Sandbox Code Playgroud)
Dev*_*per 10
这个给你
HTML
<input id="ProductId_a183060c-1030-4037-ae57-0015be92da0e" type="checkbox" value="true">
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
Ped*_*ito 10
迟到的答案,但您也可以使用 on("change")
$('#check').on('change', function() {
var checked = this.checked
$('span').html(checked.toString())
});Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="check"> <span>Check me!</span>Run Code Online (Sandbox Code Playgroud)
小智 7
只需使用单击事件我的复选框 id 是 CheckAll
$('#CheckAll').click(function () {
if ($('#CheckAll').is(':checked') == true) {
alert(";)");
}
});
Run Code Online (Sandbox Code Playgroud)
复选框单击并检查同一事件循环中的值是问题.
试试这个:
$('#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);
});
Run Code Online (Sandbox Code Playgroud)
演示:http://jsfiddle.net/mrchief/JsUWv/6/
如果您使用的是iCheck Jquery,请使用以下代码
$("#CheckBoxId").on('ifChanged', function () {
alert($(this).val());
});
Run Code Online (Sandbox Code Playgroud)
试试这个
$('#checkbox1').click(function() {
if (!this.checked) {
var sure = confirm("Are you sure?");
this.checked = sure;
$('#textbox1').val(sure.toString());
}
});
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function() {
//set initial state.
$('#textbox1').val($(this).is(':checked'));
$('#checkbox1').change(function() {
$('#textbox1').val($(this).is(':checked'));
});
$('#checkbox1').click(function() {
if (!$(this).is(':checked')) {
if(!confirm("Are you sure?"))
{
$("#checkbox1").prop("checked", true);
$('#textbox1').val($(this).is(':checked'));
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
// this works on all browsers.
$(document).ready(function() {
//set initial state.
$('#textbox1').val($(this).is(':checked'));
$('#checkbox1').change(function(e) {
this.checked = $(this).is(":checked") && !!confirm("Are you sure?");
$('#textbox1').val(this.checked);
return true;
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1319390 次 |
| 最近记录: |