Simple JavaScript search box

后端 未结 3 567
遇见更好的自我
遇见更好的自我 2021-02-10 14:30

I tried connecting the first text box, so it would turn it into a URL, and then when \'search\' is clicked, it would jump to that website, not sure if it\'s impossible, or i\'m

相关标签:
3条回答
  • 2021-02-10 15:14

    Try this JavaScript:

    function goTo()
    {
        location.href = document.getElementById('link_id').value;
    }
    

    and change the onclick in the HTML:

    <input type='text' id='link_id'>
    <input type='button' id='link' value='Search' onClick='javascript:goTo()'>
    

    Edit:

    If you want to follow the unobtrusive JavaScript way, you would move the onclick completely into your JavaScript code, and remove it in the HTML:

    document.getElementById('link').onclick = function()
    {
        location.href = document.getElementById('link_id').value;
    };
    
    0 讨论(0)
  • 2021-02-10 15:17

    Here's how I would do it:

     <input type="text" id="link-box"/>
     <input type="button" id="search-button" value="Search" 
            onclick="window.location = document.getElementById('link-box').value;"/>
    

    Of course you could do this:

     <script type="text/javascript">
          function func(){
               window.location = document.getElementById('link-box').value;
          }
     </script>
    
     onclick="func();"
    

    Or document.getElementById("search-button").onclick = function(){ window.location = document.getElementById('link-box').value;
    };

    Or last of all

     <script type="text/javascript">
           document.getElementById("search-button").addEventListener("click", function(){
                window.location = document.getElementById('link-box').value;
           });
     </script>
    
    0 讨论(0)
  • 2021-02-10 15:20

    if you want to create a form that will search google use this:

    <script type="text/javascript">
    function dos12(g1) {
    window.open('https://www.google.com/#q='+g1 +" site:linkedin.com",      'G1window');
    }
    
          </script>
      <form onsubmit="dos12(this.g1.value); return false;">
        <input type="text" name="g1" size="20" placeholder="Name" />
                <input type="submit" value="Submit" />
        Search Linkedin via Google<br />
        <br />
      </form>
    

    If you want to search a website then you will need to get the search string used. for example, if you want to search the ABN lookup site for Australia you would use the following code.

    <script type="text/javascript">
    function dos10(a1) {
    window.open('http://abr.business.gov.au/SearchByName.aspx?SearchText=' + a1,   'a1window');
    }
    
          </script>
      <form onsubmit="dos10(this.a1.value); return false;">
        <input type="text" name="a1" size="20" placeholder="Name" />
                <input type="submit" value="Submit" />
        ABN Lookup name<br />
        <br />
      </form>
    

    hope this helps. you don't need to add anything else. Just copy and paste this into notepad or your code editor and save as test.html then open with browser to test it.

    0 讨论(0)
提交回复
热议问题