从HTML输入文本字段到JavaScript并转到URL [英] Get input text field from HTML into JavaScript and go to URL

查看:189
本文介绍了从HTML输入文本字段到JavaScript并转到URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将输入文本字段从HTML转换为JavaScript并转到URL?

我正在构建一个WebPage,在输入字段中输入一些单词并将Java



我的代码是:


$ b获取此字符串并检查此字符串是否与另一个字符串相同$ b

 < input type =textname =procuraid =procura/> 
< script>
name = oForm.elements [name]。value;

if(name.equals(Advogados))
{
window.location =http://jornalexemplo.com.br/lista%20online/advogados.html
//做某事
};
< / script>

您可以给我一些灯吗?

解决方案

请注意,我在示例中使用了 jquery 库,因为它可以让侦听器更轻松地处理这些事件。



您在代码中引用了 oForm ,但在您的示例中没有看到...所以我会认为您如果你用一个特殊的ID( procura

 < DIV> 
< form method =getid =procura>
< input type =textname =procuraid =procura_textoplaceholder =Procurar/>
< / form>
< / div>

然后使用输入元素的id( procura_texto )和jquery的 val()方法,并阻止使用方法 preventDefault()):

  $(#procura)。 
//防止表单被提交
event.preventDefault();

//使用.val()获得文本框的值
name = $( #procura_texto)。val();

//比较小写字母,因为您不知道它们会输入字段
if(name.toLowerCase()== advogados)
{
//重定向用户..
window.location.href =http://jornalexemplo.com.br/lista%20online/advogados.html;
}
else
{
alert(no redirect ..(entered:+ name +));
}
});

以下是您可以玩的一个jsfiddle: http://jsfiddle.net/zwbRa/5/


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 and check if this string is equal to another, if it is go to some URL.

My code is:

 <input type="text" name="procura" id="procura" />
  <script>
  name = oForm.elements["name"].value;

  if (name.equals("Advogados"))
 {
     window.location = "http://jornalexemplo.com.br/lista%20online/advogados.html"
     //do something
 };
  </script>

Can you give me some lights?

解决方案

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/

这篇关于从HTML输入文本字段到JavaScript并转到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆