简体   繁体   English

jQuery获取具有特定参数的子元素数量

[英]jQuery get number of child elements with specific parameters

I have the following piece of code, that is being executed on submit of a form. 我有以下代码,在提交表单时执行。 Yet the following logic is incorrect as the intent was to develop a universal function that would work for all the forms in my DOM. 但是以下逻辑是不正确的,因为其目的是开发一种通用功能,该功能适用​​于DOM中的所有表单。 After failing over and over I ended up creating this function, specific to a this form, that verifies if the values of input fields in my form are the same as their custom defVal attribute, if so the form will stop submitting and an error message will pop up. 经过一遍又一遍的故障处理后,我最终创建了此函数,该函数特定于此表单,该函数验证表单中输入字段的值是否与其自定义defVal属性相同,如果是,则表单将停止提交,并且将显示错误消息弹出。

My question consists on the following: how can i check if any child elements of my form meet specific parameters, like for example being an input field and have their .val() == .attr('defVal')? 我的问题包括以下内容:如何检查表单中的任何子元素是否满足特定参数,例如作为输入字段并具有其.val()== .attr('defVal')?

I've already tried using .find(), .children() and .childNode functionalities. 我已经尝试使用.find()、. children()和.childNode功能。 Could somebody please suggest me a good solution and if possible explain your code 有人可以建议我一个好的解决方案,如果可能的话,请解释您的代码

HTML: HTML:

<div class="login" align="center">
<div class="formWrapper">
    <form action="index.php?r=backoffice" method="post">
        <input type="text" name="USERNAME" required value="username" defval="username" maxlength="16">
        <input type="password" name="PASSWORD" required value="password" defval="password" maxlength="16">
        <input type="reset" value="reset">
        <input type="submit" value="submit">
        <hr/>
        <a href="#" class="helpLink">Can't log in?</a>
    </form>
</div>
<div class="infoWrapper">
    <div class="info">

    </div>
</div>

jQuery: jQuery的:

$('div.login').ready(function(){
$('div.login form').submit(function(e){
    if( $('div.login form input').val()==$('div.login form input').attr('defVal') ){
        var numMsgs=$('div.login form').children();
        if( numMsgs.length<=6 ){
            var formHtml=$('div.login form').html();
            $('div.login form').html('<a class="sysMsg" href="#">error::invalid username/password</a>'+formHtml);
            $('div.login form a.sysMsg').addClass('errFatal').fadeIn(300).delay(5000).fadeOut(300);
        }
    e.preventDefault();
    }
});

}); });

Try (this pattern) 尝试(此模式)

$(function () {
    var elems = $(".login input[defval]");
    console.log(elems.length);
    $.each(elems, function (i, el) {
        if ($(el).val() === $(el).attr("defval")) {
            // do stuff
            console.log(i, $(el).val() === $(el).attr("defval"), $(el));
        };
    });
});

jsfiddle http://jsfiddle.net/guest271314/9578v/ jsfiddle http://jsfiddle.net/guest271314/9578v/

After sleeping over the subject I ended up managing to do the task. 在主题上睡觉之后,我最终设法完成了任务。 I've changed the html a bit. 我改变了一些HTML。 Anyway thank you for your suggestion as it was quite useful and actualy toaught me something. 无论如何,谢谢您的建议,因为它非常有用,实际上还教给我一些东西。

HTML: HTML:

<div class="login" align="center">
<div class="formWrapper">
    <form action="index.php?r=backoffice" method="post">
        <p class="jQueryErr">
        <?php
            if( !empty($_POST['USERNAME']) && !empty($_POST['PASSWORD']) ){
                phpClass::USER_login($_POST['USERNAME'],'username',$_POST['PASSWORD'],'password');
            }
        ?>
        </p>
        <input type="text" name="USERNAME" value="username" defval="username" maxlength="16">
        <input type="password" name="PASSWORD" value="password" defval="password" maxlength="16">
        <input type="reset" value="reset">
        <input type="submit" value="submit">
        <hr/>
        <a href="#" class="helpLink">Can't log in?</a>
    </form>
</div>

jQuery: jQuery的:

$('form').submit(function(e){
    var elems=$(this).find('input[defval]');
    var errWrapper=$(this).find('p.jQueryErr');
    for( var i=0; i<elems.length; i++ ){
        if( $(elems[i]).val()==$(elems[i]).attr('defVal') ){
            $(errWrapper[0]).html('<a class="sysMsg errWarning" href="#">error:: invalid username/password</a>');
            var formSysMsg=$('p.jQueryErr a.sysMsg');
            $(formSysMsg[0]).fadeIn(300).delay(5000).fadeOut(300);
            e.preventDefault();
            break;
        }else if( $(elems[i]).val().length<8 ){
            $(errWrapper[0]).html('<a class="sysMsg errWarning" href="#">error::'+ $(elems[i]).attr('defVal') +'::is too short!</a>');
            var formSysMsg=$('p.jQueryErr a.sysMsg');
            $(formSysMsg[0]).fadeIn(300).delay(5000).fadeOut(300);
            e.preventDefault();
            break;
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM