最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - changing form method with js on firefox - Stack Overflow

programmeradmin2浏览0评论

I need to change the method attribute of my form with javascript (jQuery or pure).

My form has method="post", i try to change it with:

$("#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?

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...

I need to change the method attribute of my form with javascript (jQuery or pure).

My form has method="post", i try to change it with:

$("#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?

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...

Share Improve this question edited Feb 9, 2012 at 23:42 aberti asked Feb 8, 2012 at 17:04 abertiaberti 1351 gold badge2 silver badges9 bronze badges 2
  • Is "form" the ID of your form ? Think you are missing this. – hop Commented Feb 8, 2012 at 17:52
  • yes, form was the id of the form. now i changed to form-test – aberti Commented Feb 8, 2012 at 20:01
Add a comment  | 

4 Answers 4

Reset to default 15

this is my code, and it works just fine on both 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>

Try this:

$(function(){
    $("#form").attr("method", "get");
});

You need put code after you declaration of form.

<form id="form"> ... </form>
<script>
   $("#form").attr("method", "get");
</script>

maybe you can write the code like this

$("#myPost").prop("method","get")
发布评论

评论列表(0)

  1. 暂无评论