简体   繁体   English

获取字段集中所有jquery输入的值

[英]Get the value of all the jquery inputs within a fieldset

this is the first question i ask here. 这是我在这里问的第一个问题。 I have a fieldset which has inside 4 inputs. 我有一个字段集,里面有4个输入。 I want to validate the form if at least 2 of 4 are completed. 如果要完成至少4个中的2个,我想验证表格。 I can get the value of the first input with $("#communications input").val() . 我可以使用$("#communications input").val()获得第一个输入的值。

 <fieldset class="input-group-fieldset bigger-labels" id="communications">
    <div class="row">
        <div class="columns twelve">
            <div class="input-group" id="pref-mail-wrap">
                <label>Email Address</label>
                @Html.TextBoxFor(model => model.PrefferedEmail, new { @class = "contact-group" })

            </div>
        </div>
    </div>
    <div class="row">
        <div class="columns four">
            <div class="input-group" id="pref-phone-wrap">
                <label>Telephone No</label>
                @Html.TextBoxFor(model => model.PrefferedPhoneNo, new { @class = "contact-group" })



            </div>
        </div>
        <div class="columns four">
            <div class="input-group" id="pref-sms-wrap">
                <label>SMS No</label>
                @Html.TextBoxFor(model => model.PrefferedSmsNo, new { @class = "contact-group" })

            </div>
        </div>
    </div>
    <div class="" id="pref-address-wrap">

        <div class="row fixed-width">
            <div class="columns twelve">
                <div class="input-group2">
                    <label>Address </label>

                    @Html.TextBoxFor(model => model.PostalAddress)

                </div>
            </div>

        </div>

Thank you in advance, Kostas. 预先感谢您,科斯塔斯。

You should set name attribute for all these input elements, then serialize them and check it using: 您应该为所有这些input元素设置name属性,然后对其进行序列化并使用以下命令进行检查:

if($('#communications :input').serializeArray().length >=2)

'#communications input' would be enough in your case 在您的情况下, '#communications input'就足够了

Use .filter() as below. 使用.filter()如下。

var $completedInputs = $("#communications input:text").filter(function() {
    //return the ones witn non-empty values
    return $.trim(this.value) != "";
});

if ($completedInputs.length >= 2) {
    //At least 2 inputs have been filled.   
}

$("#communications input:text") or $("#communications :text") => All inputs whose type is text $("#communications input:text")$("#communications input:text") $("#communications :text") =>所有类型为text的输入

$("#communications input") => All inputs $("#communications input") =>所有输入

you can use jquery each() for selectors returning more then 1 elements: 您可以将jquery each()用于返回超过1个元素的选择器:

$("#communications input").each(function(){
   // $(this).val(); check here what you want.
})

Try something like this. 尝试这样的事情。

var count = 0;
$('#communications input').each(function(index, item) {
    if($(item).val()!=="") {
        count ++;
    }
    return count <= 2; // break; if more than 2 have values
    });

if(count >= 2) {
    //valid
}

Read more at: https://api.jquery.com/each/ 有关更多信息, 访问: https : //api.jquery.com/each/

You can use filter() to check how many of the inputs have been given a value. 您可以使用filter()来检查有多少inputs被赋予了值。 Try this: 尝试这个:

var validInputs = $("#communications input").filter(function() {
    return !$(this).val().trim() == '';
}).length;

if (validInputs < 2) {
    // form is invalid...
}

If all the inputs have the same format and the validation is the same try to iterate through all the inputs within the fieldset 如果所有输入都具有相同的格式并且验证相同,则尝试遍历fieldset所有输入

$("#communications input").each(function(i,v){

});

Otherwise you should validate the 4 inputs individually. 否则,您应该分别验证这4个输入。

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

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