关于JavaScript表单提交-确认或取消提交对话框

JavaScript表单提交-确认或取消提交对话框

对于具有询问字段是否正确填写的警报的简单表单,我需要一个这样做的函数:

  • 使用两个选项单击按钮时显示一个警告框:

    • 如果单击“确定”,则提交表单。
    • 如果单击“取消”,则“警报”框将关闭,并可对窗体进行调整和重新提交。

我认为JavaScript确认会起作用,但我似乎不知道怎么做。

我现在的代码是:

function show_alert() {

  alert("xxxxxx");

}

<form>

  <input type="image" src="xxx" border="0" name="submit" onclick="show_alert();" alt="PayPal - The safer, easier way to pay online!" value="Submit">

</form>


海绵宝宝撒
浏览 540回答 3
3回答

largeQ

简单内联JavaScript确认就够了:<form&nbsp;onsubmit="return&nbsp;confirm('Do&nbsp;you&nbsp;really&nbsp;want&nbsp;to&nbsp;submit&nbsp;the&nbsp;form?');">不需要外部功能除非你在做验证,你可以这样做:<script>function&nbsp;validate(form)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;validation&nbsp;code&nbsp;here&nbsp;... &nbsp;&nbsp;&nbsp;&nbsp;if(!valid)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert('Please&nbsp;correct&nbsp;the&nbsp;errors&nbsp;in&nbsp;the&nbsp;form!'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;confirm('Do&nbsp;you&nbsp;really&nbsp;want&nbsp;to&nbsp;submit&nbsp;the&nbsp;form?'); &nbsp;&nbsp;&nbsp;&nbsp;}}</script><form&nbsp;onsubmit="return&nbsp;validate(this);">

qq_笑_17

function&nbsp;show_alert()&nbsp;{ &nbsp;&nbsp;if(confirm("Do&nbsp;you&nbsp;really&nbsp;want&nbsp;to&nbsp;do&nbsp;this?")) &nbsp;&nbsp;&nbsp;&nbsp;document.forms[0].submit(); &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false;}

蓝山帝景

好的,只需将代码更改为如下内容:<script>function&nbsp;submit()&nbsp;{ &nbsp;&nbsp;&nbsp;return&nbsp;confirm('Do&nbsp;you&nbsp;really&nbsp;want&nbsp;to&nbsp;submit&nbsp;the&nbsp;form?');}</script><form&nbsp;onsubmit="return&nbsp;submit(this);"> &nbsp;&nbsp;&nbsp;<input&nbsp;type="image"&nbsp;src="xxx"&nbsp;border="0"&nbsp;name="submit"&nbsp;onclick="show_alert();" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alt="PayPal&nbsp;-&nbsp;The&nbsp;safer,&nbsp;easier&nbsp;way&nbsp;to&nbsp;pay&nbsp;online!"&nbsp;value="Submit"></form>这也是运行中的代码,只是我更容易看到它是如何工作的,只需运行下面的代码就可以看到结果:function&nbsp;submitForm()&nbsp;{ &nbsp;&nbsp;return&nbsp;confirm('Do&nbsp;you&nbsp;really&nbsp;want&nbsp;to&nbsp;submit&nbsp;the&nbsp;form?');}<form&nbsp;onsubmit="return&nbsp;submitForm(this);"> &nbsp;&nbsp;<input&nbsp;type="text"&nbsp;border="0"&nbsp;name="submit"&nbsp;/> &nbsp;&nbsp;<button&nbsp;value="submit">submit</button></form>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript