0

我的 .cshtml 中有这个

@Html.DropDownListFor(m => m.Attendance.Time02InId, new SelectList(Model.ClockingLocations, "Id", "Name"), new {@class = "form-control select2-allow-clear"})

Model.ClockingLocations= 列表Clock

public class Clock
{
    public int Id { get; set; }
    public string Name { get; set; }
}

这是我准备好的文档的jquery

    var placeholder = "Select...";

    $(".select2-allow-clear").prepend("<option></option>");

    $(".select2-allow-clear").select2({
        allowClear: true,
        placeholder: placeholder,
        width: null
    });

但是,我的选择列表仍然显示我的第一项Model.ClockingLocationswhenm.Attendance.Time02InId没有价值,而不是我的占位符。

4

1 回答 1

1

尝试更新您的 JQuery 代码

var placeholder = "Select...";

$(".select2-allow-clear").prepend("<option></option>");

$(".select2-allow-clear").select2({
    allowClear: true,
    placeholder: placeholder,
    width: null
});

var placeholder = "Select...";

$(".select2-allow-clear").prepend("<option value='' selected='selected'></option>");

$(".select2-allow-clear").select2({
    allowClear: true,
    placeholder: placeholder,
    width: null
});

您也可以尝试更换

$(".select2-allow-clear").prepend("<option></option>");

$(".select2-allow-clear").prepend("<option value=''></option>").val('');

希望这对您有所帮助。

于 2016-02-21T16:46:29.890 回答