开发者

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

开发者 https://www.devze.com 2022-12-17 05:41 出处:网络
I have a plain(no fancy frameworks) javascript, which, on the submission of a form element, asks the user a question. If the answer returns true, then the script sets the form\'s action to a certain U

I have a plain(no fancy frameworks) javascript, which, on the submission of a form element, asks the user a question. 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. 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.

Here is my form:

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

Here is my javascript function:

function annoyTheUser(theForm)
{
    if(confirm("blah?"))
    {
     theForm.action = 'savequestion.asp';
     theForm.submit();
    }开发者_如何学编程
}


Your JS should look like:

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);
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号