0

为什么我的简单 javascript 验证不起作用。我想检查默认文本是否与默认值(关键字)相同,然后提交带有空参数的表单。

<form method="get" id="search_form" action="http://somesampleurl.com" onsubmit="return validation();">
<input name="s_rawwords" value="Keyword(s)" id="search_field" class="search_field" type="text">
<input name="s_freeloc" value="City, State or Zip" id="search_field2" class="search_field" type="text">
<input value="" id="search_button" type="submit">
</form>
<script type="text/javascript">
    function validation(){
        var search_key = document.getElementById("search_field").value
        alert(search_key);
        if(search_key =="Keyword(s)"){
            alert("step2");
            search_key = "";
        }
    }
</script>
4

2 回答 2

3

您的函数永远不会返回trueor false,因此它不会验证任何内容。false当输入无效并且您不希望提交表单时返回。

请参阅此处MSDN 文档

于 2013-05-31T18:59:04.710 回答
1

试试这个代码:

function validation(){
    var search_key = document.getElementById("search_field").value;
    alert(search_key);
    if(search_key =="Keyword(s)"){
        alert("step2");
        document.getElementById("search_field").value = "";
    }
    return true;
}
于 2013-05-31T19:00:18.040 回答