Javascript复选框控件
我在网上到处搜索,但找不到问题的答案。我决定在这里问一个答案。我有一个使用AJAX获取提示的代码。此代码正在使用一个按钮。我想要一个靠近这个按钮的复选框。当复选框未选中时,我想给您留言。如果不选中复选框,用户将无法继续 以下是原始代码:Javascript复选框控件,javascript,jquery,html,ajax,checkbox,Javascript,Jquery,Html,Ajax,Checkbox,我在网上到处搜索,但找不到问题的答案。我决定在这里问一个答案。我有一个使用AJAX获取提示的代码。此代码正在使用一个按钮。我想要一个靠近这个按钮的复选框。当复选框未选中时,我想给您留言。如果不选中复选框,用户将无法继续 以下是原始代码: <div class="buttons"> <div class="right"> <a id="button-confirm" class="button"><span><?php echo $
<div class="buttons">
<div class="right">
<a id="button-confirm" class="button"><span><?php echo $button_confirm; ?></span></a>
</div>
</div>
<script type="text/javascript"><!--
$('#button-confirm').bind('click', function() {
$.ajax({
type: 'GET',
url: 'index.php?route=payment/bank_transfer/confirm',
success: function() {
location = '<?php echo $continue; ?>';
}
});
});
//--></script>
这是我编辑的代码:
<div class="buttons">
<div class="right">
<input type="checkbox" value="1" /><a id="button-confirm" class="button" ><span><?php echo $button_confirm; ?></span></a>
</div>
</div>
<script type="text/javascript"><!--
$('#button-confirm').bind('click', function() {
$('#button-confirm').click(function(){
var input = $('input[type=checkbox]:checked').val();
if ( !input ){
alert('Lütfen checkboxı işaretleyin!');
} else {
}
$.ajax({
type: 'GET',
url: 'index.php?route=payment/bank_transfer/confirm',
success: function() {
location = '<?php echo $continue; ?>';
}
});
});
});
//--></script>
我的代码编辑版本不起作用。它不显示警报,当我单击按钮两次时,按钮将继续并完成该过程
我想显示一条内联警报消息,而不是弹出警报消息
谢谢你从现在起的帮助
“它不显示警报,当我单击按钮两次时,按钮将继续并完成该过程。”
您嵌套了对$('#按钮确认')的调用。在对$('#按钮确认')的调用中单击(…)
。绑定('#按钮确认')
。这意味着第一次单击时唯一发生的事情是将第二个单击处理程序绑定到同一元素。第二次单击它时,会执行两个单击处理程序,外部的一个绑定另一个单击处理程序,内部的一个实际尝试测试您的复选框并执行ajax操作
不管复选框值是什么,它都会继续处理ajax请求,原因是在您的else
语句的}
结束后,您有了$.ajax()
调用
请尝试以下方法:
$('#button-confirm').click(function () {
var input = $('input[type=checkbox]:checked').val();
if (!input) {
alert('Lütfen checkboxı işaretleyin!');
} else {
$.ajax({
type: 'GET',
url: 'index.php?route=payment/bank_transfer/confirm',
success: function () {
location = '<?php echo $continue; ?>';
}
});
}
});
我说“当我点击按钮两次”当我只点击一次时,什么也没有发生,但当我点击两次时,它会工作“复选框是否被选中并不重要。谢谢
if (!$('input[type=checkbox]').is(':checked')) {
alert('Lütfen checkboxı işaretleyin!');