我有一些jQuery代码,用于从表单中获取用户输入并将其放在电子邮件正文中。此表单中的一个字段是一个可选的复选框,其ID和名称为"成员。"我有一个隐藏字段(称为" memberChk"),用于根据是否选中复选框来存储值。如果选中该复选框,则隐藏字段应存储字符串值"是。"如果未选中该复选框,则隐藏字段应存储"否。"
好的,我更新了jQuery代码,现在值总是" No"无论是否选中该复选框。选中该复选框后,它应显示"是"在电子邮件中。我错过了什么?
这是复选框和隐藏字段的代码:
<tr><td colspan="3" valign="top"><input type="checkbox" id="member" name="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><asp:HiddenField ID="memberChk" runat="server" /></tr>
这是用于设置隐藏字段值的jQuery代码:
var member = $('#memberChk').val();
member = $('input[name="member"]:checked').size() == 0 ? "No" : "Yes";
这是构建查询字符串的代码的一部分,用于将表单条目传递给电子邮件提交:
$.ajax({
type: "GET",
url: "/Usercontrols/forms/getaquote_processor.aspx",
data: "FirstName=" + FirstName + "&LastName=" + LastName + "&Email=" + Email + "&Telephone=" + Telephone + "&BusinessName=" + BusinessName +
"&Address1=" + Address1 + "&Address2=" + Address2 + "&City=" + City + "&State=" + State + "&ZipCode=" + ZipCode + "&BusinessType=" + BusinessType +
"&OwnershipType=" + OwnershipType + "&PartnershipName=" + PartnershipName + "&MemberOf=" + MemberOf + "&CurrentInsurance=" + CurrentInsurance +
"&PolicyExpiration=" + PolicyExpiration + "&AnnualReceipts=" + AnnualReceipts + "&AnnualPayroll=" + AnnualPayroll + "&OwnOrRent=" + OwnOrRent +
"&ConstructionType=" + ConstructionType + "&YearBuilt=" + YearBuilt + "&BuildingUpdates=" + BuildingUpdates + "&Heating=" + Heating + "&HeatingYear=" + HeatingYear +
"&Cooling=" + Cooling + "&CoolingYear=" + CoolingYear + "&Plumbing=" + Plumbing + "&PlumbingYear=" + PlumbingYear + "&Electrical=" + Electrical + "&ElectricalYear=" + ElectricalYear +
"&Roofing=" + Roofing + "&RoofingYear=" + RoofingYear + "&Sprinklers=" + Sprinklers + "&PlateGlass=" + PlateGlass +
"&PlateGlassWidth=" + PlateGlassWidth + "&HomeBusiness=" + HomeBusiness + "&SeperateBuilding=" + SeperateBuilding + "&CostToReplaceBuilding=" + CostToReplaceBuilding +
"&CostToReplaceContents=" + CostToReplaceContents + "&OwnersCostToReplaceBuilding=" + OwnersCostToReplaceBuilding + "&OwnersSquareFootage=" + OwnersSquareFootage +
"&NumberOfEmployees=" + NumberOfEmployees + "&NumberOfContractors=" + NumberOfContractors + "&MAddress1=" + MAddress1 + "&MAddress2=" + MAddress2 + "&MCity=" + MCity +
"&MState=" + MState + "&MZipCode=" + MZipCode + "&VehicleMake=" + VehicleMake + "&VehicleModel=" + Model + "&VIN=" + VIN + "&Cab=" + Cab + "&Customization=" + Customization +
"&LeaseCompany=" + LeaseCompany + "&DriversName=" + DriversName + "&DriversLicense=" + DriversLicense + "&Birthday=" + BirthDay + "&CageFree=" + CageFree +
"&ConstuctionOfRuns=" + ConstructionOfRuns + "&Floors=" + Floors + "&Walls=" + Walls + "&MaxDogs=" + MaxDogs + "&AvgDogs=" + AvgDogs + "&KennelsEnclosed=" + KennelsEnclosed +
"&KennelsHeated=" + KennelsHeated + "&FenceNeeded=" + FenceNeeded + "&IsFenceAttached=" + IsFenceAttached + "&FenceReplacementCost=" + FenceReplacementCost +
"&FenceSquareFeet=" + FenceSquareFeet + "&RequireVaccinations=" + RequireVaccinations + "&CustomersUnsupervised=" + CustomersUnsupervised + "&Member=" + member + "&Comments=" + comments,
最后但并非最不重要的是,电子邮件正文中用于显示电子邮件中复选框值的部分:
#region MEMBERSHIP
//MEMBERSHIP
String Member = string.Empty;
Member = Request.QueryString["Member"];
#endregion MEMBERSHIP
#region MEMBERSHIP
sb.Append("<tr><td colspan=\"2\"><strong>Check if a you are a member of the NDGAA, PCSA, or Pet Professional Guild</strong></td></tr>");
sb.Append(string.Format("<tr><td colspan=\"2\">{0}</td></tr>", Member));
#endregion
答案 0 :(得分:0)
尝试以下代码
// work out the value
var checkVal = $('input[name="member"]:checked').length === 0 ? "No" : "Yes";
// get the checkbox
var member = $('#memberChk');
// set the checkbox value
member.val(checkVal);
现在的问题是,你不是让ASP.NET发布表单,所以隐藏字段没有按原样使用,所以我们需要获得它的价值。
var memberVal = member.val();
// obviously this step is not required you could just use the raw value we calculate above,
// this is added as I don't know the execution flow of your code
另一种方法是使用onChange处理程序,如下面的注释中的JSBin I设置
$('input[name="member"]').on('change', function(e)
{
var isChecked = $(this).is(':checked');
$('#memberChk').val(isChecked ? "Yes" : "No");
});
// then just get the value when you need it like
var member = $('#memberChk').val();
你也知道你可以使用$('form')。serialize()[http://api.jquery.com/serialize/]来获取ajax调用的所有表单值,这要求它们都有一个name属性吗?