1
$(document).ready(function(){
    $(".submit").click(function(){
                if($('.disrtrict')[0].selectedIndex<=0){
                    $("#error1").html("district field is empty");

                } 

                if($(".block")[0].selectedIndex<=0){
                    $("#error2").html("block field is empty");  
                }


                if($(".village")[0].selectedIndex<=0){
                    $("#error3").html("village field is empty");    
                }

            });
});

以上是我的 jquery 脚本。我是 jquery 的新手,并尝试学习 jquery。我的问题是我在哪里使用 return false。return true 和 false 的概念是什么,如果有人有标准检查if($(".block")[0].selectedIndex<=0) ,我们可以使用吗?请提前提供帮助。谢谢。if($(".block").val()==" ")

4

3 回答 3

2

如果它是表单提交,return true则将允许表单在稍后执行 javascript 时提交表单,并且return false不会在 javascript 之后提交表单

现在关于 jQuery 验证,是的,您可以if($(".class").val()=='')用于空或非选择

于 2013-05-21T08:37:34.450 回答
1

在您的 if 条件中,在错误消息之后,您可以使用 return false 来避免表单提交。

if($(".block").val()=="")

如果您的第一个选项值为空,则上述代码将起作用。

于 2013-05-21T08:39:32.420 回答
1

你可以这样做:

$(".submit").click(function () {
    if ($('.disrtrict').val() === '') {
        $("#error1").html("district field is empty");
        return false;
    }
    if ($(".block").val() === '') {
        $("#error2").html("block field is empty");
        return false;
    }
    if ($(".village").val() === '') {
        $("#error3").html("village field is empty");
        return false;
    }
    return true;
});

小提琴演示

于 2013-05-21T08:42:55.867 回答