admin管理员组文章数量:1414614
I am having a form in PHP. I used both onclick
and onsubmit
to call a javascript function.
On onclick
i have called the rangeValidator()
and onsubmit
i have called formValidator()
. I am checking the condition and if that is true then it is redirecting to the page which in form action
.
Code:
<form action="javascript:check();" name="frm" id="frm" onsubmit="return formValidator()">
.....
<input type="submit" onclick="return rangeValidator()">
</form>
Javascript function:
<script type="text/javascript">
function rangeValidator()
{
......
}
function formValidator()
{
var s=confirm("Are you sure about all the filled in details?");
if(s==true)
{
return true;
}
else
{
return false;
}
}
Now what i want is that, On submit it should check above condition, if that condition is true i want to check the other condition & respectively it should redirect to the page depending on true or false i.e. I want to change the action of the form to two of pages depending on value true or false of condition()
function.
To change the action I have coded as below.
function check()
{
if(confirm('Do you want to see PF share?'))
{
url='add_sal_success.php';
document.addsalsuccess.action = url;
}
else
{
url='add_sal_success_wo.php';
document.addsalsuccess.action = url;
}
}
I am having a form in PHP. I used both onclick
and onsubmit
to call a javascript function.
On onclick
i have called the rangeValidator()
and onsubmit
i have called formValidator()
. I am checking the condition and if that is true then it is redirecting to the page which in form action
.
Code:
<form action="javascript:check();" name="frm" id="frm" onsubmit="return formValidator()">
.....
<input type="submit" onclick="return rangeValidator()">
</form>
Javascript function:
<script type="text/javascript">
function rangeValidator()
{
......
}
function formValidator()
{
var s=confirm("Are you sure about all the filled in details?");
if(s==true)
{
return true;
}
else
{
return false;
}
}
Now what i want is that, On submit it should check above condition, if that condition is true i want to check the other condition & respectively it should redirect to the page depending on true or false i.e. I want to change the action of the form to two of pages depending on value true or false of condition()
function.
To change the action I have coded as below.
function check()
{
if(confirm('Do you want to see PF share?'))
{
url='add_sal_success.php';
document.addsalsuccess.action = url;
}
else
{
url='add_sal_success_wo.php';
document.addsalsuccess.action = url;
}
}
Share
Improve this question
edited Sep 3, 2013 at 11:15
Anand Jaju
asked Sep 3, 2013 at 10:41
Anand JajuAnand Jaju
4583 gold badges12 silver badges28 bronze badges
1
-
If you need to execute
rangeValidator()
every time before the form is submitted, you may have to add it to the form's event handler too, as somebody can simply hit enter on an input field and theonclick
handler is never fired. – insertusernamehere Commented Sep 3, 2013 at 10:48
1 Answer
Reset to default 3Try this
use only onsubmit="return check()"
Based on you condition you can set the action
function check()
{
var f = document.getElementById("frm");
f.setAttribute('method',"post");
if(confirm('Do you want to see PF share?'))
{
f.setAttribute('action',"yourpage.php");
}
else
{
f.setAttribute('action',"yourANOTHERpage.php");
}
return true;
}
本文标签: jqueryHow to control form action depending on condition in javascriptStack Overflow
版权声明:本文标题:jquery - How to control form action depending on condition in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745187450a2646751.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论