我正在使用cakephp并创建表单
<?php echo $form->create('User', array('action' => 'cancel'));?>
<fieldset>
<?php
echo $form->input('terms', array('type' => 'checkbox', 'label' => __('Terms and Conditions', true)));
echo $form->hidden('security', array('value' => $security));
echo $form->end(__('Cancel My Account', true));
?>
</fieldset>
现在我想要做的是,最初保持"Cancel My Account"
按钮被禁用,并且只有在选中复选框时才应启用该按钮,从而执行其各自的操作,反之亦然。我使用以下脚本来检查复选框何时被选中
<script>
$('document').ready(function(){
$('#UserTerms').click(function() {
var satisfied = $('#UserTerms:checked').val();
if (satisfied != undefined) {
$('.cancel-page div.submit input').removeAttr('disabled');
} else {
$('.cancel-page div.submit input').attr('disabled', 'disabled');
}
});
});
</script>
但这不能正常运作。取消选中该复选框后,该按钮不会被禁用。请建议如何使其发挥作用。
答案 0 :(得分:4)
建议,让我知道它是否有效:
<script type="text/javascript">
// The function is defined outside the 'document ready' event
function handleUserTermsChange() {
// Save jQuery object in variable
var $userTerms = $('#UserTerms');
// Bind 'change' event to the checkbox
$userTerms.bind('change', function() {
// Save jQuery object in variable
var $cancelAccountButton = $('.cancel-page div.submit input');
// If the checkbox is checked..
if ($userTerms.is(':checked')) {
$cancelAccountButton.removeAttr('disabled');
} else {
$cancelAccountButton.attr('disabled', 'disabled');
}
}).trigger('change');
}
$(document).ready(function(){
// The function is called in the 'document ready' event
handleUserTermsChange();
});
</script>
希望它有所帮助!