0

下面显示的 HTML 表单在 Opera(版本:9.52)中无法正常工作。该表单没有 onsubmit 属性,也没有 type=submit 的 input 元素。它只有两个 type=button 的输入元素,它们都 onclick 调用一个 js 方法,我希望用户在其中确认提交。如果我删除 confirm() 调用,它工作得很好。在所有其他浏览器(FF2、FF3、IE7)中它运行良好。

任何指针?

<script type = "text/javascript">
function userSubmit(submitStatus)
{
    // Omitted code that uses the parameter 'submitStatus' for brevity
    if(confirm('Are you sure you want to submit?'))
        document.qpaper.pStatus.value = something;
    else
        return;     
    document.qpaper.submit();
}
</script>
<form  name = "qpaper" method = "post" action = "evaluate.page">
    <input name = "inp1" type = "button" value = "Do This" class = "formbutton" onClick = "userSubmit(false)">
    <input name = "inp2" type = "button" value = "Do That" class = "formbutton" onClick = "userSubmit(true)">
</form>
4

1 回答 1

2
  1. 永远不要使用document.nameofsomething. 这是一种过时的技术,在 21 世纪的浏览器中得到了不完整的支持。对于表单使用document.forms.nameofform.

  2. 不要在按钮上使用 onclick,除非您需要 Javascript 根据按下的按钮来表现不同。如果您只想验证表单或确认提交,请<form onsubmit>改用。

    <form onsubmit="return confirm('Sure?')">
    

    这样你甚至不需要form.submit(). 如果<form onsubmit>返回 false,提交将被中止。

  3. 你甚至不需要找到那个表格。
    <button onclick>形式上将this.form
    <form onsubmit>形式将是this可变的。

于 2008-11-27T12:49:40.793 回答