如果没有输入charge_amt的输入框的值,我使用onbeforeunload jquery事件通知用户他们没有输入一个金额。在输入允许储蓄的金额之后。
<td><input type="hidden" name="charge_cc" value="0" class="">
<input type="checkbox" id="charge_cc" name="charge_cc" value="1" checked="" class=""> Charge CC? Amount $ <input type="text" id="charge_amt" name="charge_amt" value="523" size="8" maxlength="6"></td>
<script>
$(document).ready(function(){
$("input[type='text'], select, textarea").change(function(){
window.onbeforeunload = function()
{
return "You have not saved an amount to be charged.";
}
});
$("charge_amt").submit(function(){
window.onbeforeunload = null;
});
});
</script>
第二个事件用于完成按钮。
<input type='checkbox' name='reorder_comment_for_CC'> Verify Charge and Leave Comment" ?>
如果他们点击完成,但这个复选框没有被标记,我想提醒他们,如果不验证/检查,他们将无法完成事件。
也许我应该使用两个单独的事件,像这样:
<script type="text/javascript">
$("#charge_amt").keypress(function() {
if($(this).val().length > 1) {
return "You have not input an amount to be charged.";
} else {
return "Thank you for entering a charge amount, you can now save.";
}
});</script>
您可以检查用户是否在charge_amt中输入了正确的值,以及他们是否像这样勾选了复选框。您似乎对jQuery语法也有点困惑。您应该使用#the_id_of_the_element来查找单个元素。submit()
事件附加到表单上,而不是附加到字段charge_atm
上。
$("#id_of_your_form").submit(function() {
//Here you can check the checkbox
//This assumes you add the id of the checkbox to be the same as the name
var $checked = $(this).find("#reorder_comment_for_CC").is(":checked");
var $validValue = $(this).find("#charge_amt").val().length > 0;
if (!$checked) {
alert("You haven't verified charge");
return false;//prevent submitting
}
if (!$validValue) {
alert("Invalid value");
return false;
}
return true;
});