动态更改表单操作 URL 无效
当表单操作 URL 动态更改时,当表单提交时,它仍然会使用默认操作 URL,有人知道为什么吗?请看下面的一个简单示例:
<form action="test.php" method="get" id="test">
<select name="id" onChange="formSubmit(this)">
<option value="abc">abc</option>
<option value="xyz">xyz</option>
</select>
</form>
<script type="text/javascript">
function formSubmit(element){
var url = $("#test").attr("action", url);
var newParam = "&new=123";
url += "?" + element.name + "=" + element.value + newParam;
//e.g. formurl now = 'test.php?id=xyz&new=123';
$("#test").attr("action", url);
$("#test").submit();//the form will submit to test.php?id=xyz instead of the new URL
}
</script>
谢谢。
When a form action URL was changed dynamically, when the form submit, it will still use the default action URL, anybody know why? Please see a simple example below:
<form action="test.php" method="get" id="test">
<select name="id" onChange="formSubmit(this)">
<option value="abc">abc</option>
<option value="xyz">xyz</option>
</select>
</form>
<script type="text/javascript">
function formSubmit(element){
var url = $("#test").attr("action", url);
var newParam = "&new=123";
url += "?" + element.name + "=" + element.value + newParam;
//e.g. formurl now = 'test.php?id=xyz&new=123';
$("#test").attr("action", url);
$("#test").submit();//the form will submit to test.php?id=xyz instead of the new URL
}
</script>
Thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您最初在第一行为
url
变量分配空值:它应该是:
您还需要使用
get
或[0]
简写:您的函数应如下所示:
You are assigning empty value to
url
variable initially on the first line:It should be:
You also need to get
form
element withget
or[0]
shorthand:This is how your function should look: