URL - Encoding (JavaScript) method="GET"
I have a form with some input fields (6). When I click the submit button "I would like to format the URL" in such a way:
/actionname?input1|input2|input3|input4|开发者_运维技巧input5|input6
and maybe for null values:
/actionname?input1|input2||input4||input6
and maybe a time-stamp for when the user clicks the submit button.
Can this be done using javascript.
Thanks,
Chad
if you are using any java script library like jQuery, you can serialize the form which gives the data in query string format. ex: see here
It can be done via javascript but I can't understand why you would want to. You would have to use the onsubmit event of the form, loop through the form elements appending each value to a url string in the format you offered and then setting window.location
to that url string.
For users with javascript disabled, the form would still submit in the "proper" manner, which is why I can't understand why you would want to change the format of the query string.
Even if I do not see any reason why you would need this :), you can use the onSubmit event to create the proper url and redirect to it.
How about you use the XMLHTMLRequest to perform your request.
You construct your URL in a string using javascript and then set-up the XHR to submit to that URL using a GET (synchronously or a synchronously, your choice).
This way you also avoid stepping on the toes of the onsubmit
form event.
Alternatively, a simple workaround will be to construct your URL in a string and then set the form action
attribute to this string before submitting the form with the submit(
) method.
精彩评论