Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery返回false不停止执行_Javascript_Jquery - Fatal编程技术网

Javascript JQuery返回false不停止执行

Javascript JQuery返回false不停止执行,javascript,jquery,Javascript,Jquery,我有这个JQuery函数: function CheckRequired(event) { var $form = $(this); var emptyElements = $form.find('.required').filter(function() { return this.value === '' }); if(emptyElements.length > 0) { event.preventDefault()

我有这个JQuery函数:

function CheckRequired(event) {
    var $form = $(this);

    var emptyElements = $form.find('.required').filter(function() {
        return this.value === ''
    });

    if(emptyElements.length > 0) {
        event.preventDefault();

        emptyElements.addClass("EmptySelect").attr('title', 'This field is required');

        //alert(emptyElements.attr("id"));
        alert("One or more fields cannot be blank");

        return false;
    }
}
$(document).ready(function () {
    $('form').on('submit', CheckRequired);
});
我使用另一个函数调用此函数:

function CheckRequired(event) {
    var $form = $(this);

    var emptyElements = $form.find('.required').filter(function() {
        return this.value === ''
    });

    if(emptyElements.length > 0) {
        event.preventDefault();

        emptyElements.addClass("EmptySelect").attr('title', 'This field is required');

        //alert(emptyElements.attr("id"));
        alert("One or more fields cannot be blank");

        return false;
    }
}
$(document).ready(function () {
    $('form').on('submit', CheckRequired);
});
因此,它将在所有形式上被调用

它检查具有类
required

然后,我在PHP中使用了以下内容:

if($_POST) {

}
检查已发布的表单(每页都有)

但是,
返回false
并没有阻止表单的发布

更新:

“表单提交”按钮如下所示:

<input type="submit" name="submit" id="submit" value="Save" onclick="SubmitForm('#form1', '<?php echo $Settings_PathName; ?>/<?php echo $Settings_PagesPath; ?>/companies/customers/company.php?customer=<?php echo $customer["sequence"]; ?>', '.EditCustomer');" class="btn btn-default" />
因此,表单位于
company.php
上,它是从
customers.php

customers.php
有以下代码:

$(document).ready(function() {
        $('.EditCustomer').load("<?php echo $Settings_PathName;?>/<?php echo $Settings_PagesPath; ?>/companies/customers/company.php?customer=<?php echo $_GET["seq"]; ?>");
});
$(文档).ready(函数(){
$('.EditCustomer').load(“//companys/customers/company.php?customer=”);
});

因此,表单提交时不必刷新主页面,只需使用jquery重新加载即可

$('form').submit(CheckRequired);
$('form').submit(function(){
    CheckRequired(this);
});
或者你可以看到:

$(此)
在函数中不是您所想的,它是窗口对象。您必须在函数调用中传递上下文:

function CheckRequired(event, el) {
    var $form = $(el); // <---pass the context element here

    var emptyElements = $form.find('.required').filter(function() {
        return this.value.trim() === ''; //<-----should trim the value.
    });

    if(emptyElements.length > 0) {
        event.preventDefault();

        emptyElements.addClass("EmptySelect").attr('title', 'This field is required');

        //alert(emptyElements.attr("id"));
        alert("One or more fields cannot be blank");
        // return false; // it is not needed as you are using event.preventDefault();
    }
}

你添加的最后一点是你的问题。实际上,只有在
单击
事件之后,您才连接
提交
事件处理程序!这也不允许通过键盘提交表单

删除
submit
按钮上的
onclick
处理程序,并在表单上使用jQuery事件处理程序

e、 g

Html:


如果需要每个表单的特定“额外”信息(名称等),可以将它们作为数据属性注入表单,并使用jQuery表单对象上的
.data()
获取它们

笔记:
  • 您应该能够获取与单击表单相关的div etc,而不是传递它。要给出一个精确的示例,需要更多的HTML
CheckRequired正在从我的PHP页面调用抱歉-请参阅我的更新,首先我误解了您:)
event.preventDefault()
应该足以阻止
提交
继续。不需要返回
。奇怪的是,您的代码对于您使用它的方式看起来是正确的。我假设警报正在触发?我看到警报正常,但一旦解除警报,它将继续执行内部的php代码,如果($_POST){}@charlie您能确认浏览器控制台中没有任何错误。现在显示了调用代码,您可以看到它已经按照您的建议进行了操作,并且具有此
(提交的表单)的正确上下文,这就是我所想的。我在哪里调用SubmitForm函数?在您的另一段代码中,您已经做到了。因此,情况也会类似。如果您可以显示页面/表单输出HTML的示例(例如,从浏览器保存,而不是PHP源代码),我可以对其进行适当的模拟。我可以从源页面看到的代码是:$(document).ready(function(){$('.EditCustomer').load(“/pages/companys/customers/company.PHP?customer=340”);$('.tab')。each(function(){$(this).on(“单击”,function(){$('.EditCustomer').load(“/pages/companys/customers/”+$(this.attr('href')+“?customer=340”);});});好的……由于页面内容看起来是动态加载的,所以采用不同的方法。使用Chrome浏览器F12工具-DOM inspector并复制body元素的HTML,将其放入您的问题中。谢谢。