我使用此处描述的方法解决了它,尝试使用 select 是一个糟糕的决定。答案来源:
jsfiddle:http:
//jsfiddle.net/nQFxU/3/
代码
HTML
<div id="btn-group">
<button type="button" data-name="uid0" class="btn" data-toggle="btn-input" data-target="#puBtn0" value="uid0">
<img src="http://placehold.it/50">
<br>Randy</button>
<input type="hidden" id="puBtn0" />
<button type="button" data-name="uid1" class="btn" data-toggle="btn-input" data-target="#puBtn1" value="uid1">
<img src="http://placehold.it/50">
<br>Dick</button>
<input type="hidden" id="puBtn1" />
<button type="button" data-name="uid2" class="btn" data-toggle="btn-input" data-target="#puBtn2" value="uid2">
<img src="http://placehold.it/50">
<br>Jane</button>
<input type="hidden" id="puBtn2" />
<button type="button" data-name="uid3" class="btn" data-toggle="btn-input" data-target="#puBtn3" value="uid3">
<img src="http://placehold.it/50">
<br>Alice</button>
<input type="hidden" id="puBtn3" />
<button type="button" data-name="uid4" class="btn" data-toggle="btn-input" data-target="#puBtn4" value="uid4">
<img src="http://placehold.it/50">
<br>John</button>
<input type="hidden" id="puBtn4" />
</div>
JS
$(document).ready(function () {
$('[data-toggle="btn-input"]').each(function () {
var $this = $(this);
var $input = $($this.data('target'));
var name = $this.data('name');
var active = false; // Maybe check button state instead
$this.on('click', function () {
active = !active;
if (active) $input.attr('name', name).val($this.val());
else $input.removeAttr('name').removeAttr('value');
$this.button('toggle');
});
});
});
结论
这行得通,麻烦少了很多,而且实施起来也很容易。这样做的缺点是在我.html?ID1=ID1&ID2=ID2
希望的表单提交结果中.html?users=ID1+ID2+ID3