HTML代碼:將表單的複選框值傳遞爲JSON?
<form class="form-horizontal" id="addpersons" style="padding:20px;">
<fieldset class="scheduler-border">
<!-- Form Name -->
<legend class="scheduler-border">Information</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="fname">First Name</label>
<div class="col-md-5">
<input id="fname" name="firstName" type="text" placeholder="First Name" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="lname">Last Name</label>
<div class="col-md-5">
<input id="lname" name="lastName" type="text" placeholder="Last Name" class="form-control input-md">
</div>
</div>
<!-- Multiple Checkboxes (inline) -->
<div class="form-group">
<label class="col-md-4 control-label" for="professionalservices">Do you offer any of the services?</label>
<div class="col-md-4" style="width:70%; margin-left:34%;">
<label class="checkbox-inline" for="professionalservices-0">
<input type="checkbox" name="professionalservices" id="professionalservices-0" value="1">
BI services
</label>
<label class="checkbox-inline" for="professionalservices-1">
<input type="checkbox" name="professionalservices" id="professionalservices-1" value="2">
Resell vendor's services
</label>
<label class="checkbox-inline" for="professionalservices-2">
<input type="checkbox" name="professionalservices" id="professionalservices-2" value="3">
Consulting Services
</label>
<label class="checkbox-inline" for="professionalservices-3">
<input type="checkbox" name="professionalservices" id="professionalservices-3" value="4">
Other servies
</label>
<label class="checkbox-inline" for="professionalservices-4">
<input type="checkbox" name="professionalservices" id="professionalservices-4" value="5">
No services
</label>
</div>
</div>
</fieldset>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<a href="javascript:addperson();" class="btn btn-success">Apply Now</a>
</div>
</div>
</form>
的Json代碼:
<script type="text/javascript">
function addperson(){
alert("hello");
var persons = JSON.stringify({
"firstName": $('#fname').val(),
"lastName":$('#lname').val(),
"Services":$('#services').val()
});
alert("test values are"+persons);
console.log(persons);
$.ajax({
type: "POST",
contentType: 'application/json',
url: baseurl+"addperson/add",
data: persons,
dataType:"text",
success:successmethod,
error: function(data,status) {
alert("Error "+status);
}
});
}
function successmethod(data){
alert("sucessfully stored values");
}
</script>
在上面的代碼是能夠利用每個文本框的值,但是當涉及到複選框,我想裏面retrive多個複選框值json,並希望將它傳遞給Ajax.Can任何人都可以告訴我如何將多個複選框值輸入到Json中?任何幫助都將被讚賞..
請不要downvote問題不必要的一切都是在已被要求從people..then得到答案,爲什麼這到底是downvoted問題的問題很好。 – User2413 2014-10-08 13:14:41
是的,它得到了答案,你發現自己說*「它不工作」*和答題者回復*「我試過了,它給了一個JSON字符串,我的格式,你需要」*?這意味着問題不完整,他們只是假設需要什麼。 – 2014-10-08 14:08:29