Get input text field from HTML into JavaScript and go to URL

前端 未结 2 1339
礼貌的吻别
礼貌的吻别 2021-01-29 09:26

How get input text field from HTML into JavaScript and go to URL?

I\'m building one WebPage where you type some word into the input field and the Java gets this string a

相关标签:
2条回答
  • 2021-01-29 09:42

    Note, I've used the jquery library here in my example as it makes setting up the listeners to handle these events easier.

    You're referencing oForm in your code, but I don't see that in your examples... so I would think that you will find this easier if you wrap the in a form tag, with a particular id (procura)

    <div>
        <form method="get" id="procura">
            <input type="text" name="procura" id="procura_texto"  placeholder="Procurar"/>
        </form>
    </div>
    

    Then capture the result using the id of the input element (procura_texto), and jquery's val() method and prevent the form from being submitted using the method preventDefault():

    $("#procura").on("submit", function(event){     
    
        // prevent form from being submitted
        event.preventDefault();
    
        // get value of text box using .val()
        name = $("#procura_texto").val();
    
        // compare lower case, as you don't know what they will enter into the field
        if (name.toLowerCase() == "advogados")
        {
            // redirect the user.. 
            window.location.href = "http://jornalexemplo.com.br/lista%20online/advogados.html";
        }
        else
        {
            alert("no redirect..(entered: " + name + ")");
        }
    });
    

    here's a jsfiddle for you to play with: http://jsfiddle.net/zwbRa/5/

    0 讨论(0)
  • 2021-01-29 10:04

    Use window.location.href = "your url".

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