我有一个带有几个复选框的简单 HTML 表单。我的目标是将此数据(复选框的值)传递给一个 jQuery 函数,并将这些值输入到一个字符串中,该字符串将通过 Ajax 传递给一个 php 脚本。这是我的html:
<table class="ui-widget-content" border="0" style="width: 100%;">
<tr>
<td width="252px;">PVC's Required</td>
<td align="left">
<input type="checkbox" id="pvc" name="pvc" value="abc" />abc<br />
<input type="checkbox" id="pvc" name="pvc" value="def" />def<br />
<input type="checkbox" id="pvc" name="pvc" value="ghi" />ghi<br />
<input type="checkbox" id="pvc" name="pvc" value="jkl" />jkl<br />
<input type="checkbox" id="pvc" name="pvc" value="NONE" />NONE<br />
</td>
</tr>
</table>
jQuery:
$("#addTechDetails")
.button()
.click(function(){
var pvc = $("#input[ type= 'checkbox']");
var pvcData = [];
pvc.filter(':checked').each(function(){
pvcData.push($(this).val());
})
pvc=pvcData.join(',');
//initial info required to process form
var newOrExisting = $("#newOrExisting").val();
var numCircuits = $("#numCircuits").val();
var str = "newOrExisting="+newOrExisting+"&numCircuits="+numCircuits+"&pvc="+pvc;
//ajax request
$.ajax({
type : "POST",
cache : false,
url : "ajax/addTechDetails.php",
data : str,
complete : function(xhr, result){
if (result != "success") return;
var response = xhr.responseText;
$("#result").html(response);
}
})
});
即使选中了某些选项,上面的输出也如下所示:'','','','',