在函数运行后更改了这些属性后,对具有特定属性的当前输入数进行计数 [英] Count the current number of inputs with a specific property after a function has run which has changed these properties

查看:331
本文介绍了在函数运行后更改了这些属性后,对具有特定属性的当前输入数进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关此问题:

我很乐意帮助您完成此功能

    $('input[type=submit]').prop('disabled', true).addClass('disabled');

    var required = $('[required]');

    required.bind('change keyup', function()
    {
        var flag = 0;
        required.each(function()
        {   
            if ($(this).not(':checkbox, :radio').val() || $(this).filter(':checked').val())
            {
                flag++;
            }
        });

        if (flag == required.length)
        {
            $('input[type=submit]').prop('disabled', false).removeClass('disabled');
        }
        else
        {
            $('input[type=submit]').prop('disabled', true).addClass('disabled');
        }
    });

这里发生的是表单的提交按钮一直处于禁用状态,直到具有"required"属性的所有字段都具有某种值(或已被选中等).

更大图片的一部分,另一个jQuery函数(效果很好)根据选择框的值隐藏了其中的某些字段.

有效地,当那些字段被隐藏时,必需的属性应被删除.删除了必填属性后,我希望上面的函数仅对剩余的必填字段进行计数(required.length),但实际上,它对所有这些字段进行了计数,即使那些不再具有必需属性的字段也是如此. >

对此有任何想法吗?

解决方案

您一次又一次地比较同一组元素.您需要在每次检查期间重新选择所需的内容.

更改

required.each(function()

收件人

$([required]).each(function()

您正在选择所有元素,然后遍历它们,无论它们现在具有什么属性.您希望jQuery重新选择现在具有required属性的元素,以便获得更新的列表.然后,这应该会按预期执行.

Related to this question: Using jQuery, keep a form's submit button disabled until all of the required textbox, radio, checkbox elements have a value (or are checked)

I was very kindly helped to put together this function

    $('input[type=submit]').prop('disabled', true).addClass('disabled');

    var required = $('[required]');

    required.bind('change keyup', function()
    {
        var flag = 0;
        required.each(function()
        {   
            if ($(this).not(':checkbox, :radio').val() || $(this).filter(':checked').val())
            {
                flag++;
            }
        });

        if (flag == required.length)
        {
            $('input[type=submit]').prop('disabled', false).removeClass('disabled');
        }
        else
        {
            $('input[type=submit]').prop('disabled', true).addClass('disabled');
        }
    });

What's happening here is that the submit button of a form is staying disabled until all fields with the "required" property have some sort of value (or have been checked, etc.).

Part of a bigger picture, another jQuery function (which works fine) is hiding some of these fields depending on the value of a select box.

Effectively, when those fields are hidden, the required property should be removed. With the required property being removed, I would expect the above function to count (required.length) only the remaining required fields, but in fact, it counts all of them -- even those ones that no longer have the required property.

Any ideas on a way around this?

解决方案

You are comparing the same set of elements over and over again. You need to re-select the ones that you need during each check.

Change

required.each(function()

To

$([required]).each(function()

You are selecting all the element and then looping through them regardless of the properties that they now have. You want to have jQuery re-select the elements that now have the required property so that you get an updated list. This should then perform as you expect.

这篇关于在函数运行后更改了这些属性后,对具有特定属性的当前输入数进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆