Changing the action of a form with JavaScript/jQuery

后端 未结 6 778
借酒劲吻你
借酒劲吻你 2020-11-29 19:40

I\'m having an issue that is driving me crazy. I\'m trying to modify the openid-selector to support facebook. I\'m using RPXNow as my provider so it requires the form to be

相关标签:
6条回答
  • 2020-11-29 20:10

    jQuery (1.4.2) gets confused if you have any form elements named "action". You can get around this by using the DOM attribute methods or simply avoid having form elements named "action".

    <form action="foo">
      <button name="action" value="bar">Go</button>
    </form>
    
    <script type="text/javascript">
      $('form').attr('action', 'baz'); //this fails silently
      $('form').get(0).setAttribute('action', 'baz'); //this works
    </script>
    
    0 讨论(0)
  • 2020-11-29 20:10

    just to add a detail to what Tamlyn wrote, instead of
    $('form').get(0).setAttribute('action', 'baz'); //this works

    $('form')[0].setAttribute('action', 'baz');
    works equally well

    0 讨论(0)
  • 2020-11-29 20:15

    I agree with Paolo that we need to see more code. I tested this overly simplified example and it worked. This means that it is able to change the form action on the fly.

    <script type="text/javascript">
    function submitForm(){
        var form_url = $("#openid_form").attr("action");
        alert("Before - action=" + form_url);   
        //changing the action to google.com
        $("#openid_form").attr("action","http://google.com");
        alert("After - action = "+$("#openid_form").attr("action"));
        //submit the form
        $("#openid_form").submit();
    }
    </script>
    
    
    <form id="openid_form" action="test.html">
        First Name:<input type="text" name="fname" /><br/>
        Last Name: <input type="text" name="lname" /><br/>
        <input type="button" onclick="submitForm()" value="Submit Form" />
    </form>
    

    EDIT: I tested the updated code you posted and found a syntax error in the declaration of providers_large. There's an extra comma. Firefox ignores the issue, but IE8 throws an error.

    var providers_large = {
        google: {
            name: 'Google',
            url: 'https://www.google.com/accounts/o8/id'
        },
        facebook: {
            name: 'Facebook',
            form_url: 'http://wikipediamaze.rpxnow.com/facebook/start?token_url=http://www.wikipediamaze.com/Accounts/Logon'
        },  //<-- Here's the problem. Remove that comma
    
    };
    
    0 讨论(0)
  • 2020-11-29 20:31

    Just an update to this - I've been having a similar problem updating the action attribute of a form with jQuery.

    After some testing it turns out that the command: $('#myForm').attr('action','new_url.html');

    silently fails if the action attribute of the form is empty. If i update the action attribute of my form to contain some text, the jquery works.

    0 讨论(0)
  • 2020-11-29 20:31

    Use Java script to change action url dynamically Works for me well

    function chgAction( action_name )
    {
    
     {% for data in sidebar_menu_data %}
    
         if( action_name== "ABC"){ document.forms.action = "/ABC/";
         }
         else if( action_name== "XYZ"){ document.forms.action = "/XYZ/";
         }
    

    }

    <form name="forms" method="post" action="<put default url>" onSubmit="return checkForm(this);">{% csrf_token %} 
    
    0 讨论(0)
  • 2020-11-29 20:35

    You can actually just use

    $("#form").attr("target", "NewAction");
    

    As far as I know, this will NOT fail silently.

    If the page is opening in a new target, you may need to make sure the URL is unique each time because Webkit (chrome/safari) will cache the fact you have visited that URL and won't perform the post.

    For example

    $("form").attr("action", "/Pages/GeneratePreview?" + new Date().getMilliseconds());
    
    0 讨论(0)
提交回复
热议问题