我无法从jquery多选下拉列表中检查选项 即使能够在多选列表中找到该选项之后。 请告诉我我错过了什么。谢谢!
//this is my multi-select dropdown<br/>
$("#ddlCountries").multiselect();
var country = "USA";
//I tried below, but it did not work
//$("#ddlCountries").multiselect("widget").find(":checkbox[value='" + country + "']").attr("checked", true);
回应@Bhavin: 我按你做的,仍然没有得到预期的结果。请参阅下文。
$(document).ready(function () {
$("#ddlCountries").multiselect();
$("#ddlCountries").multiselect('uncheckAll');
}
function ddlRole_OnChange(){
//client object of ddlRoles combobox
var roles = $(this).data('tComboBox');
//selected item from ddlRoles
var selectedRole = roles.text();
if (selectedRole.toLowerCase() === "admin"){
//custom yes/no dialog box (first param is Title, second param is Question, third param is callback function)
YesNoJqueryDailogBox('Admin Configuration:', 'Need Admin Role ?', 'selectForAdminRole');
}
}
function selectForAdminRole(){
$(document).ready(function (){
$("#ddlCountries").multiselect();
$("#ddlCountries").multiselect('uncheckAll');
var country = "USA";
$("#ddlCountries").multiselect("widget").find(":checkbox[value='" + country + "']").prop('checked', true);
});
}
回应@Ziv Weissman MVC视图(.cshtml)
<tr>
<td align="left" valign="middle" width="30%">
@if (ViewBag.Roles != null)
{
@(
Html.Telerik().ComboBox().Name("ddlRole") @*server-side combobox name*@
.BindTo((IEnumerable<SelectListItem>)ViewBag.Roles) @*server-side ddlRole binding in page load*@
.ClientEvents(events => events.OnChange("ddlRole_OnChange")) @*server-side ddlRole change event*@
)
}
</td>
</tr>
<tr>
<td align="left" valign="middle" width="15%" height="40" >
Countries <span class="mandatory">*</span>
</td>
<td align="left" valign="middle" width="20%" >
@if (ViewBag.Countries != null)
{
@*server-side ddlCountries dropdown binding in page load*@
@Html.DropDownList("ddlCountries", (IEnumerable<SelectListItem>)ViewBag.Countries)
}
</td>
</tr>
解决:检查选项后刷新ddlCountries下拉列表解决了问题。
var countries= $("#ddlCountries").multiselect('uncheckAll');
countries.children('option').each(function (){
var countryNameText = $(this).text();
if (countryNameText == "admin"){
$(this).attr('selected', 'selected');
}
});
$("#ddlCountries").multiselect("refresh");
谢谢你们所有人!
答案 0 :(得分:1)
你必须刷新multiSelect,以及为什么在selectForAdminRole函数中添加$(document).ready
$("#ddlCountries").find("option[value=" + ce + "]").prop("selected", true)
$("#ddlCountries").multiselect("refresh")
你可以使用这个
$("#ddlCountries").val("USA");
$("#ddlCountries").multiselect("refresh")
答案 1 :(得分:0)
如果我将onload事件放两次或setTimeout工作。 我使用第一个选项作为解决方法,它很难看,但工作正常。
$(document).ready(function() {
$(document).ready(function() {
$('#id').multiselect();
});
});
$(document).ready(function() {
setTimeout(function() {
$('#id').multiselect();
}, 500);
});
jQuery:1.8.3
Bootstrap:3.0.3
多选:0.9.13