Javascript警报无法在网站上运行

时间:2017-03-15 22:23:00

标签: javascript html forms submit alert

我在javascript中尝试了几种不同的东西,但似乎没有任何东西可以在我的网站上运行。我正在尝试在提交表单时弹出警报。这就是我所拥有的

spark.yarn.am.memory=1g,spark.yarn.am.memoryOverhead=384

这是网站:http://webpages.uncc.edu/~kjardine/MC_Portfolio/contact.html

2 个答案:

答案 0 :(得分:0)

您几乎就在那里,关键是您已经拥有<form>标记的代码从第1行开始,因此您应该将onsubmit=""属性添加到该代码中。以下是修改后的代码:

<form onsubmit="return confirm('Do you really want to submit the form?');">
    Name:<br>
    <input type="text" name="name" required><br>
    Email:<br>
    <input type="email" name="email" required><br>
    Phone (Format: 999-999-9999):<br>
    <input type="tel" name="phone" required pattern="\d{3}[\-]\d{3}[\-]\d{4}"><br>
    Nature of comment:<br>
    <input type="checkbox" name="comment" value="Question"> Question
    <input type="checkbox" name="comment" value="Business Inquiry"> Business Inquiry
    <input type="checkbox" name="comment" value="Comment"> Comment
    <input type="checkbox" name="comment" value="Other"> Other <br>
    Comment:<br>
    <textarea></textarea><br>
    <input type="submit" value="Submit">
</form>

答案 1 :(得分:0)

onsubmit应该放在顶部的表单元素中,就像评论者说的那样。并且应删除当前的表单元素。

也就是说,您也可以为该网站检查一次“不再显示弹出窗口”选项,现在弹出窗口将被禁用,直到您再次启用弹出窗口,例如清除Chrome缓存。

<form onsubmit="return confirm('u sure?');">
    <input>...
</form>

我还建议不要使用onsubmit作为HTML参数,而是在实际的<script></script块中编写一个js处理程序来进行确认。

window.onload = function () {
    document.getElementById("myForm").onsubmit = function onSubmit(form) {
        return confirm('u sure?');
    }
}

在这种情况下,请务必将id="myForm"添加到表单元素中。