这里我有一个表单,我需要在按下“ enter”键时提交表单,我将按钮类型指定为“ submit”,以便在按下Enter时提交表单,同时,我调用带有验证和确认的函数,它不要求确认消息并关闭并提交,我的预期结果是,当我按Enter时,它应显示确认/验证消息,然后再次按Enter,则应提交。下面是我的代码
<form name="form1" id="form1" action="SizeMaster.php" enctype="multipart/form-data" method="post">
<input type="text" class="form-control" name="txtSizeName" value="" />
<button type="submit" onClick="return fnSave()" class="btn btn-success waves-effect"><i class="material-icons">save</i><span>SAVE</span></button>
</form>
function fnSave(){
if(form1.txtSizeName.value==""){
alert("Please Enter size");
return false;
}
(!confirm("Are You Sure Want to confirm the Request?")){
return false;
}
form1.action="SizeMaster.php?Mode=SAVE";
}
答案 0 :(得分:1)
$(document).on("submit", "form#form1", function (event) {
event.preventDefault();
//put your conditions here
if(form1.txtSizeName.value==""){
alert("Please Enter size");
return false;
}
});
//OR
$(document).keypress(function (e) {
if (e.which == 13) {
$('form#form1').submit();
}
});
答案 1 :(得分:0)
请在onClick事件上调用您的函数。并且在确认之前您缺少if
。
尝试一下。
<form
name="form1"
id="form1"
onSubmit="return fnSave()"
action="SizeMaster.php"
enctype="multipart/form-data"
method="post"
>
<input type="text" class="form-control" name="txtSizeName" value="" />
<button type="submit" class="btn btn-success waves-effect">
<i class="material-icons">save</i><span>SAVE</span>
</button>
</form>
<script>
function fnSave() {
if (form1.txtSizeName.value == "") {
alert("Please Enter size");
return false;
}
if (!confirm("Are You Sure Want to confirm the Request?")) {
return false;
}
form1.action = "SizeMaster.php?Mode=SAVE";
}
</script>
答案 2 :(得分:-1)
使用onsubmit
<form onsubmit="return fnSave()" name="form1" id="form1" action="SizeMaster.php" enctype="multipart/form-data" method="post">
<input type="text" class="form-control" name="txtSizeName" value="" />
<button type="submit" class="btn btn-success waves-effect"><i class="material-icons">save</i><span>SAVE</span></button>
</form>
答案 3 :(得分:-1)
您应该在表单元素的onSubmit属性上添加动作侦听器
{
"aps": {
"alert": {
"title": "Hey!? Checkout my custom notification",
"subtitle": "Custom notification subtitle",
"body": "Description of custom notification"
},
"sound": "default",
"category": "CustomPush",
"badge": 1,
"mutable-content": 1
},
"Employee": {
"Name": "John Doe",
"Designation": "Manager"
}
}
然后在<from onSubmit='fnSave()' > ...
函数上,您将在fnSave()
变量上获取事件对象。然后调用this.preventDefault()停止提交表单。之后,您可以进行验证和其他操作