简体   繁体   English

动态形式为Action的Javascript不会在确认对话框的(true)上提交

[英]Javascript, with dynamic form Action, does not submit on (true) of confirm dialog

I have a plain(no fancy frameworks) javascript, which, on the submission of a form element, asks the user a question. 我有一个普通的(没有花哨的框架)javascript,在提交表单元素时,它会向用户询问一个问题。 If the answer returns true, then the script sets the form's action to a certain URL (which has some server-side logic inside it) and calls the sumbit method of that form. 如果答案返回true,则脚本会将表单的操作设置为某个URL(其中包含一些服务器端逻辑)并调用该表单的sumbit方法。 Or at least, in theory, thats what its meant to do! 或至少在理论上,这就是它的意义! But it doesnt.. it just doesn't do anything. 但是它没有..它什么也没做。 It seems to submit the form, but if it did, the server-side logic in the other file (which has been set as the action property's value) would ensure the user is taken somewhere else. 似乎提交了表单,但是如果提交了表单,则另一个文件中的服务器端逻辑(已设置为action属性的值)将确保用户被带到其他地方。

Here is my form: 这是我的表格:

<form name='myForm' id='myForm' method='post' onSubmit='annoyTheUser(this);'>

Here is my javascript function: 这是我的JavaScript函数:

function annoyTheUser(theForm)
{
    if(confirm("blah?"))
    {
     theForm.action = 'savequestion.asp';
     theForm.submit();
    }
}

Your JS should look like: 您的JS应该看起来像:

function annoyTheUser(theForm)
{
    if(confirm("blah?"))
    {
        theForm.action = 'savequestion.asp';
        return true;
    }
    else
        return false;
}

Can you add the part that makes the form submit also. 您也可以添加使表单提交的部分吗? The script looks fine. 该脚本看起来不错。 Only that one seems suspicious. 只有那一个看起来可疑。

Try this 尝试这个

<form name='myForm' id='myForm' method='post' onSubmit='return annoyTheUser(this);'>

And the script should be 脚本应该是

function annoyTheUser(theForm)
{
if(confirm("blah?"))
{
theForm.action = 'savequestion.asp';
return(true);
}
else
{
return(false);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM