我有一个选择框:
<select id="issue-users_id" class="form-control" name="Issue[users_id]">
<optgroup label="user">
<option value="16">user</option>
<option value="24">robo</option>
</optgroup>
<optgroup label="group">
<option value="15">123123</option>
</optgroup>
</select>
提交表单时,我希望有这样的数组:
Issue[users_id][user] = value (16, 24)
or Issue[users_id][group] = value (15)
这意味着我想获得optgroup的价值
答案 0 :(得分:1)
一切皆有可能是手动准备数据并将其发送到服务器。提交获取表单数据并替换Issue.user_id
param:
var selected = $('#issue-users_id').find(":selected"),
optgroup = selected.closest('optgroup');
if ( optgroup.attr('label') == 'group' ) {
form_data.Issue.users_id = {
group: selected.val()
}
} else {
form_data.Issue.users_id = {
user: selected.val()
}
}
$.ajax(/* send form */);