0

如何计算有多少输入收集此功能,如果不是 4,则显示带有一些信息的警报?

 $('#dialog').on('show', function () {
        $('form#form_save_account input:visible').each(function(idx, $input) {
            $('#dialog .bank-data .' +  $input.name + ' .value').text($input.value);
        });
    });
4

2 回答 2

0

So if you want to check how many elements are found by your selector expression, You could use the length property, which is defined on jQuery objects:

$('#dialog').on('show', function () {
    var inputs = $('form#form_save_account input:visible');

    if (inputs.length != 4) {
        // Handle error
    }

    // Further work
}

If you also want to make sure that each input element has a non-empty value, you could filter the inputs variable before checking the length:

inputs = inputs.filter(function(i, input) { 
    return $(input).val().trim() != ""; 
});

You might also want to have a look at the jQuery form validation plugin, as it can automate such tasks:

http://docs.jquery.com/Plugins/Validation

Edit: Modified as I misunderstood your question in the beginning. Also added some more information.

于 2013-01-17T01:16:53.753 回答
-1
var index = 0 
$('#dialog').on('show', function () {
    $('form#form_save_account input:visible').each(function(idx, $input) {
        index += 1;
    });
    if (index !== 4) { // do alert }
});
于 2013-01-17T01:16:42.480 回答