javascript - Simple Form Submit Code - Stack Overflow

admin2025-04-18  0

I want to create a simple form with only an input textbox and search button.

A person would type in the search term in the box and press search.

All that will do is take their search term and add it to the end of the url.

So if search term is "good italian food" the submit button would send the user to http://domain/find/good italian food

Whats the simplest way to do this?

Thank you!

I want to create a simple form with only an input textbox and search button.

A person would type in the search term in the box and press search.

All that will do is take their search term and add it to the end of the url.

So if search term is "good italian food" the submit button would send the user to http://domain/find/good italian food

Whats the simplest way to do this?

Thank you!

Share Improve this question edited Dec 11, 2010 at 4:32 Sudantha 16.2k45 gold badges109 silver badges162 bronze badges asked Dec 11, 2010 at 4:21 vitalypvitalyp 6914 gold badges12 silver badges23 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3
<html>
  <head>
    <script type='text/javascript'> 
      function Search() {
        var keyword = document.getElementById("keyword").value; 
        var url = "http://yourwebsite./find/"+keyword; 
        window.location = url; 
        return false;
      }
    </script>
  </head>
  <body>
    <form name="search">
      <input type="text" name="keyword" id="keyword" />
      <input type="button" name="btnser" onclick="Search()" value="Search" />
    </form>
  </body>
</html>

I'd also suggest using encodeURI() to make it 'safe'.

http://www.w3schools./jsref/jsref_encodeuri.asp

use javascript and html form

create a form with a button and text box in the post of the form call the javascript function to create the url with the user input text

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
  function redirect(){  
    var s = document.getElementbyID("txt").value;
    var url="http://url./" + s;
    window.location = url;
  }
</script>
</head>
<body>
  <form>
    <input type=''></input>
  </form>
</body>
</html>

you could override the action attribute within the onsubmit event: Example

HTML

<form action="#" onsubmit="send(this)" method="post" target="_blank" >
    <input id="text"/>
    <input type="submit" value="submit"/>
</form>

JavaScript

function send( form){
    form.action = location.href + '/'+encodeURI(document.getElementById('text').value);
}

Also, overriding the form action, or redirecting the user, within the onsubmit event will allow you to use a true submit button.

This gives the user the ability to just hit the enter key once they finish typing to submit the search without you having to write extra logic listening for the the enter key within the textbox to submit the search.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744964925a277139.html

最新回复(0)