我页面上有一个表单使用此
multiselect plugin
<!-- Target employees tab -->
<div class="tab-pane fade" id="employees" role="tabpanel" aria-labelledby="employees-tab">
<form class="" action="/assessments/<%=assessment._id%>/employees" method="post" id="employees-form">
<div class="my-2">
<div class="form-group">
<label for="users">Select employees to assess</label>
<div class="row" id="users">
<div class="col-5">
<select name="from[]" id="search" class="form-control" size="8" multiple="multiple">
<% users.forEach(function(user){ %>
<option value="<%=user._id%>"><%=user.firstName%> <%=user.lastName%></option>
<% }) %>
</select>
</div>
<div class="col-2">
<button type="button" id="search_rightAll" class="btn btn-block"><i class="fas fa-angle-double-right"></i></button>
<button type="button" id="search_rightSelected" class="btn btn-block"><i class="fas fa-angle-right"></i></button>
<button type="button" id="search_leftSelected" class="btn btn-block"><i class="fas fa-angle-left"></i></button>
<button type="button" id="search_leftAll" class="btn btn-block"><i class="fas fa-angle-double-left"></i></button>
</div>
<div class="col-5">
<select name="to[]" id="search_to" class="form-control" size="8" multiple="multiple"></select>
</div>
</div>
</div>
</div> <!-- end of .my-2 -->
<div class="text-center">
<button type="submit" class="btn btn-primary text-center" style="width: 100px;" id="employees-form-submit"><i class="fas fa-chevron-circle-right"></i> Next</button>
</div>
</form>
</div> <!-- end of employees tab -->
由以下js发起:
$(document).ready(function($) {
$('#search').multiselect({
search: {
left: '<input type="text" name="q" class="form-control" placeholder="Search..." />',
right: '<input type="text" name="q" class="form-control" placeholder="Search..." />',
},
fireSearch: function(value) {
return value.length > 3;
}
});
我试图通过ajax请求提交此表单,但是当我尝试
serialize()
from[]
to[]
选择框。有什么想法吗?
我在其他地方使用过这些多重选择,但不是通过ajax路由,这是我第一次遇到这个问题。
我的ajax是:
$('#employees-form-submit').on('click', function(e) {
e.preventDefault();
var form = $('#employees-form');
var formData = form.serialize();
var url = form.attr('action');
$originalItem = form.parent('#employees').siblings('#summary')
console.log(formData);
$.ajax({
url: url,
data: formData,
type: 'PUT',
originalItem: $originalItem,
success: function(data) {
target = this.originalItem.find('#employees-summary');
data.targetEmployees.forEach(function(emp) {
target.find("ul").append(
`<li>${emp.firstName} ${emp.lastName} - email: ${emp.eMail}</li>`
)
})
showTab('summary-tab')
}
});
});