Jquery的:
<script type="text/javascript">
function becomepartner()
{
var locations = [];
$('#geographicregion :selected').each(function(i, selected){
locations[i] = $(selected).val();
});
alert("locations ========"+locations);
var partners = JSON.stringify({
"firstName": $('#fname').val(),
"lastName":$('#lname').val(),
"geographicRegion":locations,
});
$.ajax({
type: "POST",
contentType: 'application/json',
url: baseurl+"becomepartner/add",
data: partners,
dataType:"text",
success:successmethod,
error: function(data,status) {
//alert("Error "+status);
}
});
}
HTML:
<form class="form-horizontal" id="becomepartnerfrm" style="padding:20px;" onsubmit="becomepartner(); return false;">
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="fname">First Name</label>
<div class="col-md-5">
<div class="required-field-block">
<input id="fname" name="firstName" type="text" placeholder="First Name" pattern="[A-Za-z ]{3,20}" oninvalid="setCustomValidity('First Name should be in range of 3 to 20 characters(only alphabets are allowed')" onchange="try{setCustomValidity('')}catch(e){}" class="form-control input-md" required="required">
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
</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">
<div class="required-field-block">
<input id="lname" name="lastName" type="text" placeholder="Last Name" pattern="[A-Za-z ]{3,20}" oninvalid="setCustomValidity('Last Name should be in range of 3 to 20 characters(only alphabets are allowed')" onchange="try{setCustomValidity('')}catch(e){}" class="form-control input-md" required="required">
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
</div>
</div>
<!-- Select Multiple -->
<div class="form-group">
<label class="col-md-4 control-label" for="geolocation">Business Locations/Regions</label>
<div class="col-md-5">
<select id="geographicregion" name="geographicRegion" class="form-control" multiple="multiple">
<option value="APAC">APAC</option>
<option value="EMEA">EMEA</option>
<option value="LATAM">LATAM</option>
<option value="Middle East">Middle East</option>
<option value="NA">NA</option>
<option value="Public Sector">Public Sector</option>
<option value="All">All</option>
</select>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<button type="submit" class="btn btn-success">Apply Now</button>
</div>
</div>
</form>
在上面的代码中我有多个选择框..我正在从中选择三个值并将其置于函数内...但只有最后一个值保存在数据库内而不是在多个选择框中选择的所有三个值。请给它一点亮点??
答案 0 :(得分:0)
在选择框中发布多重值需要为数组
更改选择框名称 geographicRegion []
以下是表单提交帮助时在客户端获取价值的过程
<form onsubmit="functionname($this)"></form>
在客户端
function functionname($form)
{
var values = {};
$.each($form.serializeArray(), function(i, field) {
values[field.name] = field.value;
});
alert(values.field_name);
}
在Ajax中使用 async:false 属性来传输数据。
答案 1 :(得分:0)
我们可以通过以下代码保存数据库中的所有值:
var geolocations=$('#geographicregion :selected').map(function(){return $(this).val(); }).get();
var totalloc="";
for(var i=0;i<geolocations.length;i++)
{
totalloc+=geolocations[i]+",";
}
alert("locations ========"+locations);
var partners = JSON.stringify({
"firstName": $('#fname').val(),
"lastName":$('#lname').val(),
"geographicRegion":totalloc,
});
答案 2 :(得分:0)
让它变得简单
var partners = JSON.stringify({
"firstName": $('#fname').val(),
"lastName":$('#lname').val(),
"geographicRegion": $('#geographicregion').val()
});
已编辑的代码
var geographicregion = $('#geographicregion').val().join();
var partners = {
"firstName": 'qwewqe',
"lastName":'qwerewrwqe',
"geographicRegion": geographicregion,
};