How to use JavaScript to change the form action [d

2019-01-04 13:48发布

This question already has an answer here:

My current code is as follows:

<div class="fr_search">   
  <form action="/"  accept-charset="UTF-8" method="post" id="search-theme-form">
  .......
  </form>
</div>

Now I want to write a function to change the form action and method, when a condition is met. How do I write this code?

For example,

function test() {
   if (selectedIndex === 1)....
} // How do I write this code?

5条回答
神经病院院长
2楼-- · 2019-01-04 13:55
 // i assume that you want to change form action based on drop-down choice 
 function change_action(form1){
if(form1.mydropdown_name.selectedIndex===1){
    form1.action = "aaaa.php";
}else{
         form1.action = "bbbb.php";
    }
 }

 //how to use
 <select name='mydropdown_name' id='mydropdown_id' onchange="change_action(this.form)"><option value="1">first</option><option value="2">second</option></select>
 //this way you do not have to provide js function with form name.
查看更多
看我几分像从前
3楼-- · 2019-01-04 13:57
function chgAction( action_name )
{
    if( action_name=="aaa" ) {
        document.search-theme-form.action = "/AAA";
    }
    else if( action_name=="bbb" ) {
        document.search-theme-form.action = "/BBB";
    }
    else if( action_name=="ccc" ) {
        document.search-theme-form.action = "/CCC";
    }
}

and your form need to have name is this case

<form action="/"  accept-charset="UTF-8" method="post" name="search-theme-form" id="search-theme-form">
查看更多
男人必须洒脱
4楼-- · 2019-01-04 13:59

I wanted to use Javascript change a form's action, so I could have different submit inputs within the same form linking to different pages. I also had the added complication of using Apache rewrite to change example.com/page-name into example.com/index.pl?page=page-name. I found that changing the form's action caused example.com/index.pl (with no page parameter) to be rendered, even though the expected URL (example.com/page-name) was displayed in the address bar. To get around this, I used Javascript to insert a hidden field to set the page parameter. I still changed the form's action, just so the address bar displayed the correct URL.

function setAction (element, page)   
{ 
  if(checkCondition(page))
  {
    /* Insert a hidden input into the form to set the page as a parameter. 
     */ 
    var input = document.createElement("input"); 
    input.setAttribute("type","hidden"); 
    input.setAttribute("name","page"); 
    input.setAttribute("value",page);   
    element.form.appendChild(input); 

    /* Change the form's action. This doesn't chage which page is displayed, 
     * it just make the URL look right. 
     */ 
    element.form.action = '/' + page;   
    element.form.submit(); 
  }
} 

In the form:

<input type="submit" onclick='setAction(this,"my-page")' value="Click Me!" />

Here are my Apache rewrite rules:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^/(.*)$ %{DOCUMENT_ROOT}/index.pl?page=$1&%{QUERY_STRING}

I'd be interested in any explaination as to why just setting the action didn't work.

查看更多
家丑人穷心不美
5楼-- · 2019-01-04 14:04

Try this:

var frm = document.getElementById('search-theme-form') || null;
if(frm) {
   frm.action = 'whatever_you_need.ext' 
}
查看更多
我只想做你的唯一
6楼-- · 2019-01-04 14:15

If you're using jQuery, it's as simple as this:

$('form').attr('action', 'myNewActionTarget.html');
查看更多
登录 后发表回答