我有一个选择定义为 -
<div class="form-group">
<div class="col-md-10">
<select name="selected-attendees" id="selected-attendees" class="form-control" multiple size="5">
@foreach (Customer attendee in Model.AttendeeList)
{
var attendeeName = String.Format("{0}, {1}", attendee.FullName, attendee.Email);
<option class="selectable-option" value="@attendee.Id">@attendeeName</option>
}
</select>
</div>
</div>
当用户点击添加按钮时,这就是操作 -
$('#attendees-btn-add').click(function () {
if ($('#selectedAttendee').val() == '') {
alert("Select a customer to be added to the attendee list.");
} else {
$('#selected-attendees').append($('<option>', {
class: 'selectable-option',
value: $('#attendeeId').val(),
text: $('#selectedAttendee').val()
}));
$('#selectedAttendee').val("");
$('#attendeeId').val("");
}
});
我需要确定用户是否已添加到选择并显示消息。
答案 0 :(得分:1)
您可以使用特定值查询<option>
,并在有任何结果时显示消息:
if ($('#selected-attendees option[value="' + $('#attendeeId').val() + '"]').length) {
//display message about duplicate
}
答案 1 :(得分:0)
尝试将此条件发送到.click
函数:
var exists = $("#selected-attendees").find('option[value=' + $('#attendeeId').val() + ']').length >0;
if(exists)
{
//do if exists
}
else
{
//do because not exists
}