【问题标题】:jquery target input value inside divs with class checked已检查类的 div 内的 jquery 目标输入值
【发布时间】:2014-03-16 20:11:52
【问题描述】:

我在报价单中寻找一些供应商,每个供应商都有一个复选框元素来勾选应该联系的。供应商位于类名称供应商复选框的 div 中,此外,在选择供应商时,供应商复选框 div 会附加“checkedsupplier”或“uncheckedsupplier”类名。

<div class="supplier-checkbox checkedsupplier">

<input type="hidden" name="supplier-email" class="supplieremail" value="{$item->email}" />
                                    </div> <!--end supplier checkbox-->

foreach 供应商 div 我在 div 中获得一个隐藏的输入字段,其中包含供应商电子邮件作为其值。想法是,在提交表单时,我会检查所有具有“checkedsupplier”类名的 div,并为每个获取其中的输入值,将所有值保存在 onelong 变量中并将其回显到电子邮件的 To 字段中以进行复制给每个供应商的报价单。

使用 jquery,我设法切换类名和背景效果,显示勾选或取消勾选以下内容。

<script> 
    $(".supplier-checkbox").click(function(){
    $(this).toggleClass('checkedsupplier uncheckedsupplier')
    //$(this).children( ".supplieremail" ).attr("checked")
});
</script>

任何人都可以给我任何关于如何通过类名进行foreach并使用jquery获取每个具有该类名的div内的每个输入的值的指针。我正在研究它,但发现 .each 用于循环 .val 用于值,if (bar) 用于条件。但我还没有足够的经验来把东西放在一起。自从网络专业毕业后,他作为一名初级开发人员已经工作了 7 个月,这是一个完全不同的领域。

在 app.js 文件中,我以这种方式获取表单值: `

v_country:$('#country').val(),
v_quotationtel:$('#quotationtel').val(),
                v_mobile:$('#mobile').val(),
                v_quotationemail:$('#quotationemail').val(),
                v_quotemefor:$('#quotemefor').val(),
    v_quotemessage:$('#quotemessage').val()
                //this line is what i'm trying to do to get input values and store them in one var to pass them to php form that sends email $( "checksupplier" ).each(function( i ) {

         }`

感谢堆栈中的每个人!我相信你们在我的学习和工作中都帮助了我。

伊恩

** 更新** 修改如下:

 <script>
        $(function(){
            $( ".crew-member-con" ).click(function(){
                $(this).toggleClass('whitebg greybg');
                $(this).toggleClass('crew-checked crew-unchecked');
                $(this).toggleClass('grey white');
                if($(this).children('input').prop('checked') == true){
                    $(this).children('input').prop('checked', false);
                }else{
                    $(this).children('input').prop('checked', true);
                }

                var selectedMembers='';
                $('.selected-members').each(function(){
                    if($(this).is(':checked')){
                        selectedMembers +=$(this).val()+', ';
                    }
                   //alert(emails);
                });
                if(selectedMembers != ''){
                    selectedMembers = selectedMembers.slice(0,-2);
                }
                $('#exam-member span').html(selectedMembers);
                console.log(selectedMembers);
});         

        });

【问题讨论】:

  • 收听复选框时使用on('change', function.change(function,而不是.click(function

标签: php jquery class html get


【解决方案1】:

这是一个带有一些注释的示例。

 $('.myform').submit(function () {
    getEmailAddresses();
    return false; 
}

function getEmailAddresses () {
    // a place to hold them
    var emails = new Array();
    // what will be after 'mailto'
    var emailsString = "";

    // get 'em
    $('.checkedsupplier').each(function () {
        // get the hidden email input value
        var email = $(this).find('.supplieremail').val();
        // check, just in case;
        if (email) {
            // push the value into our array
            emails.push(email); 
        }
    });

    //put them all together into a string, split by a semi-colon
    emailsString = emails.join(';');

    // finally, stuff our mail to link. 
    // Not sure what you're plan is here but 'emailsString' has all of the values you eed.
    $('.mailtolink').attr('href', 'mailto:'+emailsString);

}

【讨论】:

  • 谢谢伙计!我最终接受了这两个答案的一部分,并且由于您的贡献,结果非常好!现在它已经被进一步修改以更好地适应它的目的。 :) 你让别人的阳光闪耀!
【解决方案2】:
<script> 
$(".supplier-checkbox").click(function(){
$(this).toggleClass('checkedsupplier uncheckedsupplier')
//$(this).children( ".supplieremail" ).attr("checked")
var emails='';
$('.supplieremail').each(function(){
   emails +=$(this).val();
});

$('#mailfieldstextarea').val(emails);
});
</script>

我希望这会有所帮助。

【讨论】:

  • 感谢您的反馈伙伴。我认为在您建议的解决方案中从逻辑上讲,它不会区分 .supplieremail 输入的父 div 类为checkedsupplier 或未经检查的供应商。因为我在您的代码第 7 行之后添加了一个警报:警报(电子邮件);单击供应商时,警报会显示电子邮件,它会自动重新显示下一个供应商电子邮件,并重申将每个供应商电子邮件填充到变量中,而无需在第一个供应商之后单击任何其他供应商。它需要一个条件 if 来检查父 div 类是否已检查供应商
  • 你能和父子元素分享你所有的html吗?
  • 感谢您的回答,它帮助我解决了问题,感谢您!
猜你喜欢
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多