如果输入val == something,请转到URL

时间:2013-12-13 12:33:01

标签: javascript jquery html

所以,我找到了this JSFiddle example。在JSFiddle工作得很好,问题是,即使我从“advogados”(仅测试)搜索任何!=,浏览器也会转到:http://www.site.com/index.html?procura=teste

没有jQuery冲突,没有HTML问题。

这是JS

$("#procura").on("submit", function(event){     

    // prevent form from being truely submitted
    event.preventDefault();

    // get value of text box
    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 + ")");
    }
});

2 个答案:

答案 0 :(得分:0)

您没有定义变量名称。 http://jsfiddle.net/zwbRa/59/

var name = $("#procura_texto").val();

答案 1 :(得分:0)

如果您的javascript位于HTML 之前的某个位置,则<form>您的$("#procura")将为空集,因此提交操作不会受到任何限制。请尝试以下代码:

 <html>
     <head>
         <script type="text/javascript" src="/path/to/your/jquery.js"></script>
         <script type="text/javascript">
             $(function() {
                 // This code will be run if your document is completely
                 // parsed by the browser, thus all below elements are
                 // accessible

                 $('#procura').on('submit', ....);
             });
         </script>
    </head>
    <body>
         <form id="procura">...</form>
    </body>
</html>

$(function() {})也称为$(document).ready(function() {}),(documentation