如果输入val=,请转到URL

Go to URL if input val == something

本文关键字:URL 输入 val 如果      更新时间:2023-09-26

所以,我找到了这个JSFiddle示例。在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 + ")");
    }
});

如果您的javascript在<form>之前位于HTML中的某个位置,则$("#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() {}),(文档)

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

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