简体   繁体   English

如何使用jQuery获取每个选中的复选框

[英]How to get each checked checkbox using jQuery

I have the following: 我有以下内容:

PHP/HTML CODE: PHP / HTML代码:

<?php
$c = 1;
    foreach($this->contactos as $contacto){
?>
        <div class="uk-form-row">
        <label for="contactopadrino<?php echo $c; ?>" class="uk-form-label"><?php echo $contacto->email; ?></label>

        <input class="contactopadrinos" name="contactopadrino[]" id="contactopadrino<?php echo $c; ?>" type="checkbox" value="<?php echo $contacto->email; ?>" />
        </div>

<?php 
$c++;
}
?>      

jQuery CODE: jQuery代码:

     function validarEnviarDescuento(){  
        $('#errorofertacontainerdescuento').css('display','none');
        $('#errorofertadescuento').html('');
        var validar = 0;
        var vacio = 0;
        for(var e=1; e<6; e++){
            var email = $("#email_contactopadrino"+e).val();    
            if(!validarEmail(email) && email != ''){
                validar++;
            }
            if(email != ''){
                vacio++;
            }
        }

        if(!vacio){
             $('input:radio:checked').each(function () { 
                var $this = $(this);

                if ($(this).prop('checked')) {
                    return true;
                }
            });

            $('#errorofertadescuento').append('<li>' + '<?php echo JText::_('COM_CSTUDOMUS_ERROR_SIN_SELECCIONAR'); ?>' + '</li>');
            $('#errorofertacontainerdescuento').css('display','block');
            return false;
        }
        if(validar){ 
            $('#errorofertadescuento').append('<li>' + '<?php echo JText::_('COM_CSTUDOMUS_ERROR_EMAIL_INCORRECTO'); ?>' + '</li>');
            $('#errorofertacontainerdescuento').css('display','block');
            return false; 
        }
        else{ 
            return true; 
        }

 }

Im trying to go through each input and if one is checked it should return true and submit but what I have done is not working. 我试图通过每个输入,如果一个被检查它应该返回true并提交,但我所做的不起作用。

You can use :checked with selector to get all radio buttons those are checked. 您可以使用:checked选择器以获取所有选中的单选按钮。

$('input:radio:checked')

Description : Matches all elements that are checked or selected, jQuery docs 描述 :匹配所有检查或选择的元素, jQuery文档

If you want to check if atleast one radio is checked then you can use length 如果要检查是否检查了至少一个无线电,则可以使用长度

if($('input:radio:checked').length)
{
}

For iterating through checked radio you can use each 对于迭代已检查的无线电,您可以使用每个

$('input:radio:checked').each(function(){
  alert(this.value);
});

You don't need to use each . 您不需要使用each instead use cobination :checked and length 而是使用cobination :checkedlength

return $('input:checkbox:checked').length;

It will return true if anyone of the checkbox button has checked 如果选中任何复选框按钮,它将返回true

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

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