试图让以下工作,但无法真正找到阻止选择发生的错误。
<select multiple="multiple" class="inputbox" name="keywords[]" id="keywords">
<option value="4">4</option>
<option value="11">Car</option>
<option value="8" selected="selected">Cat</option>
<option value="9">Elephant</option>
<option value="6">Man</option>
<option value="10">Mouse</option>
<option value="1">One person</option>
<option value="2">People</option>
<option value="5">Woman</option>
</select>
<input type="text" class="inputbox" value="" id="keywords_new" name="keywords_new">
<a href="#" id="addkeyword">Add keyword</a>
jQuery
$(document).ready(function(){
$('#addkeyword').click(function (event) {
event.preventDefault();
var newkeyword = $('#keywords_new').val();
var keyselect = $('#keywords');
keyselect.each(function () {
alert(newkeyword);
if ($(this).text() == newkeyword){
alert(newkeyword);
$(this).attr("selected", "selected").multiselect("refresh");
}
});
});
});