简体   繁体   中英

Construct GET url from java script and submit

On click of search button in my form, i want to set some param to url and submit it.

Below is my javascript

function validateSubmitSearch(form) {   
  if(form.elements["iAccept"].checked == true) {
    form.query.value=form.search_query.value;
    form.action = "vendor/search";
    form.method = "GET";
    form.submit();
  }
}

This javascript is returning url as http://localhost:8080/Project/vendor/search?query=xxx&search_query=xxx&search=Search

Instead i need it as http://localhost:8080/Project/vendor/search?query=xxx

How this can be done ?

EDIT:

I cannot remove form elements search_query and search

Here is HTML code

<form class="searchform" onSubmit="validateSubmitSearch(this)">
  <input name="query" type="hidden" />
  <input name="search_query" class="textbox" type="text" />
  <input type="checkbox" id="iAccept" name="iAccept" value="I ACCEPT">
  <input name="search" class="button" value="Search" type="submit" /></td>
</form>

When you submit a form with `method="GET", all the input fields in the form will be included as URL parameters. If you want some of them to be left out, you need to remove those inputs from the form.

function validateSubmitSearch(form){

    if(form.elements["iAccept"].checked == true)
        {
            form.query.value=form.search_query.value;
            form.action = "vendor/search";
            form.method = "GET";
            form.seach_query.parentNode.removeChild(form.search_query);
            form.search.parentNode.removeChild(form.search);
            form.submit();
        }
    }
}

Also, you should disable the default form submission by returning false from the onsubmit code.

<form class="searchform" onsubmit="validateSubmitSearch(this); return false;">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM