如何用JS控制表单的POST目标页面?

wzwen 2011-07-15 03:09:19
先说下我的情况

支付页面:
两个单选框:支付宝、手机支付
输入金额框
一个提交按钮

想通过js来控制提交到的POPST目标页面

因为支付宝和手机支付的付款接受页面是不同的,路径也不同

如果用户选择的是“支付宝”充值,就跳到:alipay/alipayto.php
如果用户选择的是“手机支付”充值,就跳到:moblie/dodirectpayment_token.php




html代码:
<FORM name="form1" method="post">
支付宝:<input type="radio" name="payMethod" value="alipay" checked/>
手机支付:<input type="radio" name="payMethod" value="mobile" />
充值金额:<input type="text" name="money" maxlength="30" size="10" value="10" />
<input name= "btn" type="submit" value="立即充值" onClick="javascript:btn_onsubmit(); ">
</FORM>



JS代码:
function btn_onsubmit() 
{
var val = GetRadioValue("payMethod");
var page = "";

switch(val)
{
case "alipay":
page = "alipay/alipayto.php";
break;

case "mobile":
page = "moblie/dodirectpayment_token.php";
break;
}

window.form1.action = page;
window.form1.submit;

}

//循环提取单选值-可以忽略不看
function GetRadioValue(RadioName){
var obj;
obj=document.getElementsByName(RadioName);
if(obj!=null){
var i;
for(i=0;i<obj.length;i++){
if(obj[i].checked){
return obj[i].value;
}
}
}
return null;
}
</script>



现在不知道为什么,JS的这两句:

window.form1.action = page;
window.form1.submit;

似乎不起作用?也就是我点按钮,JS操作的方法里
page变量可以成功赋值,但没有跳转到POST目标页面
...全文
259 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzwen 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hongmei85 的回复:]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title> n……
[/Quote]

你这个测试通过

奇怪,我复制到我的网页中,不起作用,被js搞死了……
zell419 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wzwen 的回复:]

引用 3 楼 guotongyu1 的回复:

提交按钮type改成button


果然可以了,不过表单的传输方式变成了get过去,不是post,有点晕
[/Quote]
document.form1.method= "POST ";
试试 。
hongmei85 2011-07-15
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title> new document </title>
<meta name="keywords" content="">
<meta name="description" content="">
</head>
<script type="text/javascript">
<!--
function btn_onsubmit()
{
var val = GetRadioValue("payMethod");
var page = "";

switch(val)
{
case "alipay":
page = "alipay/alipayto.php";
break;

case "mobile":
page = "moblie/dodirectpayment_token.php";
break;
}

window.form1.action = page;

}

//循环提取单选值-可以忽略不看
function GetRadioValue(RadioName){
var obj;
obj=document.getElementsByName(RadioName);
if(obj!=null){
var i;
for(i=0;i<obj.length;i++){
if(obj[i].checked){
return obj[i].value;
}
}
}
return null;
}
//-->
</script>
<body>
<FORM name="form1" method="post" onsubmit="return btn_onsubmit()">
支付宝:<input type="radio" name="payMethod" value="alipay" checked/>
手机支付:<input type="radio" name="payMethod" value="mobile" />
充值金额:<input type="text" name="money" maxlength="30" size="10" value="10" />
<input name= "btn" type="submit" value="立即充值">
</FORM>
</body>
</html>
wzwen 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 guotongyu1 的回复:]

提交按钮type改成button
[/Quote]

果然可以了,不过表单的传输方式变成了get过去,不是post,有点晕
guotongyu1 2011-07-15
  • 打赏
  • 举报
回复
提交按钮type改成button
wzwen 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hch126163 的回复:]

document.form1.submit();
[/Quote]

不行呀
hch126163 2011-07-15
  • 打赏
  • 举报
回复
document.form1.submit();

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧