我制作了一个html表单,我想让它需要输入,然后在js确认之前提交,这样它就可以正常工作了:
<form action="someAction" method="post">
<input type="text" required/>
<button type="submit" onclick="return confirm('sure to submit?');">test</button>
</form>
如果未确认,则表明我们未在所需的输入标签中输入任何内容。然后,将出现一个信息框告诉您。 但是只要确认,信息框就会消失。
我该怎么做?
答案 0 :(得分:0)
如果我了解您想要的代码,则这样...
已更新 使用html验证,如果需要
function validate(){
var collection = document.forms["myForm"].elements;
for(var k in collection){
if(collection[k].type === 'text'){
if(collection[k]['value']!== '') {
console.log(collection[k]['value']);
}else{
return false;
}
}
}
return true;
}
function onSubmit(){
if(confirm('Confirm this action')) return true; // submit the form
// else, manualy validate in js each form element value
console.log(`The form is valide : ${validate()}`);
alert('Some Info to dislay'); // display info
return false; // prevent submit event
}
<form action="someAction" method="post" name="myForm" onSubmit="return onSubmit();">
<!-- validate in html -->
<input name="data" required type="text" />
<input name="blabla" type="text" required pattern="[A-Za-z]{3}" />
<input type="submit" value="test"/>
</form>
答案 1 :(得分:0)
我找到了路,分享给每个人。
$( "form" ).submit(function( event ) {
if (!confirm("Sure to submit?")) {
return false;
}
});
它将首先检查所需的输入,然后触发Submit事件并确认。