如何在多个选择下拉列表中获取所选选项的列表。我在这里使用这个jQuery插件:searchable-option-list
我试过这个,但没有给它选择它。只有第一个值记录在db中。我想保存所有选定值的集合。 这是jquery脚本:
<script type="text/javascript">
$(function() {
$('#companytype').searchableOptionList();
});
var items = [];
$('#companytype option:selected').each(function(){ items.push($(this).val()); });
var result = items.join(', ');
console.log(result);
</script>
这是多选:
<select name="companytype" id="companytype" class="mid first-input-div" multiple="multiple">
<optgroup label="UPSTREAM">
<option value="Peter">Peter Griffin</option>
<option value="Lois">Lois Griffin</option>
<option value="Chris">Chris Griffin</option>
<option value="Meg">Meg Griffin</option>
<option value="Stewie">Stewie Griffin</option>
</optgroup>
<optgroup label="MID-STREAM">
<option value="Cleveland">Cleveland Brown</option>
<option value="Joe">Joe Swanson</option>
<option value="Quagmire">Glenn Quagmire</option>
</optgroup>
<optgroup label="DOWNSTREAM">
<option value="Oil and transportation">Oil and transportation</option>
<option value="Bunkering">Bunkering</option>
<option value="Brokering">Brokering</option>
</optgroup>
</select>
我正在使用Laravel,这是插入代码:
public function store(CompanyRequest $companyRequest)
{
$company = new Company;
if($companyRequest->isMethod('post')){
$company->user_id = Auth::user()->id;
$company->companyname = $companyRequest->companyname;
$company->companytype = $companyRequest->companytype;
$company->save();
return redirect()->route('companyindex')->with('message', 'Your question has been posted.');
}else{
return redirect('company-create')->withErrors($companyRequest)->withInput();
}
}
答案 0 :(得分:1)
我猜您需要将name="companytype"
更改为name="companytype[]"
。将元素转换为数组,因为此处的选择是多个。并在服务器脚本中检索相同内容,request in array format
内嵌一些字符串,然后将其插入数据库。
我相信,你的PHP改变应该是这样的。 (更改为数组后)
$company->companytype = $companyRequest->companytype;
将上述声明更改为
$company->companytype = implode(",",$companyRequest->companytype);
这应该对你有帮助。