javascript 在 Firefox 上使用 js 更改表单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9198028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
changing form method with js on firefox
提问by aberti
I need to change the method attribute of my form with javascript (jQuery or pure).
我需要使用 javascript(jQuery 或 pure)更改表单的方法属性。
My form has method="post", i try to change it with:
我的表单有 method="post",我尝试用以下方法更改它:
$("#submit-button").click(function(){
var url = $('input[id=url]').val();
var method = $('#method option:selected').val();
$("#form-test").attr("action", url);
$("#form-test").attr("method", method);
$("#form-test").submit();
});
This code works on Chrome and I8 but not on Firefox. The action is set correctly and also method variable contains "get" or "post" correctly. Any idea?
此代码适用于 Chrome 和 I8,但不适用于 Firefox。操作设置正确,方法变量也正确包含“get”或“post”。任何的想法?
SOLVED:I was using an old version of jquery (copy&paste fault), i've upgraded to 1.7.1 and now it works, with the same code...
已解决:我使用的是旧版本的 jquery(复制和粘贴错误),我已经升级到 1.7.1,现在它可以工作了,代码相同......
回答by Churk
this is my code, and it works just fine on both IE/FF/Chrome
这是我的代码,它在 IE/FF/Chrome 上都可以正常工作
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function changeMethod() {
$("#myPost").attr("method", "get");
}
</script>
<form method="post" id="myPost">
<input type="text" name="abc" id="abc" value="Something" />
<input type="submit" value="submit" onclick="changeMethod()" />
</form>
回答by wanovak
Try this:
试试这个:
$(function(){
$("#form").attr("method", "get");
});
回答by Jones
You need put code after you declaration of form.
您需要在声明表单后放置代码。
<form id="form"> ... </form>
<script>
$("#form").attr("method", "get");
</script>
回答by lutfi adnan
maybe you can write the code like this
也许你可以写这样的代码
$("#myPost").prop("method","get")