使用JQuery将复选框值附加到查询字符串

时间:2014-09-22 18:34:56

标签: jquery checkbox query-string

我正在处理一些对我来说有点混乱的jQuery代码。我还是jQuery的新手,我之前没有尝试使用它构建一个查询字符串(我更熟悉C#/ .NET)。

我想要做的是将复选框的值附加到查询字符串,以便可以在另一个页面上用于电子邮件提交。如果选中该复选框,则应提交其值。如果取消选中该复选框,则不应提交任何内容。

注意:大部分代码不是我的,而是我必须处理的其他人。

这是jQuery代码(我想设置的查询字符串参数是成员:[这里放什么]):

        if(success == 0)
        {
            if($('.catchTB').val().length == 0)
            {
                //CHECKBOX GROUPS
                var personalInsurance = '', specialtyInsurance = '', commercialInsurance = '';
                $('.personalInsurance:checked').each(function() {
                    if(personalInsurance.length > 0)
                        personalInsurance = personalInsurance + ", ";
                    personalInsurance = personalInsurance + $(this).val();
                });   
                $('.specialtyInsurance:checked').each(function() {
                    if(specialtyInsurance.length > 0)
                        specialtyInsurance = specialtyInsurance + ", ";
                    specialtyInsurance = specialtyInsurance + $(this).val();
                });  
                $('.commercialInsurance:checked').each(function() {
                    if(commercialInsurance.length > 0)
                        commercialInsurance = commercialInsurance + ", ";
                    commercialInsurance = commercialInsurance + $(this).val();
                });

              //this is the code that sets up the parameters of the query string, and member is the one I want to set with the checkbox value
                $.get("/UserControls/Forms/GetAQuoteProcessor.aspx", { fname: $('.firstName').val(), lname: $('.lastName').val(), email: $('.email').val(), telephone: $('.telephone').val(), address: $('.address1').val(), address2: $('.address2').val(), city: $('.city').val(), ddl: $('.ddl').val(), zipcode: $('.zipcode').val(), personalInsurance: personalInsurance, specialtyInsurance: specialtyInsurance, commercialInsurance: commercialInsurance, member: [what to put here], comment: $('.comment').val(), catchTB: $('.catchTB').val() },
                  function(data){
                $('#getAQuote').css('display', 'none');
                $('.response').html(data);
                  });
            }
        }
    });

这是复选框:

        <td  colspan="3" valign="top"><input type="checkbox" class="personalInsurance checkBox" id="member" value="Member of the NDGAA, PCSA, or Pet Professional Guild" /> Check if a you are a member of the NDGAA, PCSA, or Pet Professional Guild</td>

1 个答案:

答案 0 :(得分:0)

要查看是否选中了复选框,请使用.is(':checked'),如下所示:

var checked = jQuery('#member').is(':checked');

它将返回一个布尔值。