如何检查 Jquery 多 select 下拉列表中的选项?
How to check an option in Jquery multi-select dropdown list?
我无法从 jquery 多 select 下拉列表中选中一个选项
即使能够在 multi-select 列表中找到该选项。
请告诉我我缺少什么。谢谢!!
//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);
Response @ Bhavin:
I did as per you, still not getting expected result. Please see below.
$(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);
});
}
Response @ Ziv Weissman
MVC view (.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>
Solved: Refreshing ddlCountries dropdown after checking the option solved the problem.
var countries= $("#ddlCountries").multiselect('uncheckAll');
countries.children('option').each(function (){
var countryNameText = $(this).text();
if (countryNameText == "admin"){
$(this).attr('selected', 'selected');
}
});
$("#ddlCountries").multiselect("refresh");
Thanks all of you!
如果我放置两次 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
您必须刷新 multiSelect,以及为什么要在 selectForAdminRole 函数中添加 $(document).ready
$("#ddlCountries").find("option[value=" + ce + "]").prop("selected", true)
$("#ddlCountries").multiselect("refresh")
你可以使用这个
$("#ddlCountries").val("USA");
$("#ddlCountries").multiselect("refresh")
我无法从 jquery 多 select 下拉列表中选中一个选项 即使能够在 multi-select 列表中找到该选项。 请告诉我我缺少什么。谢谢!!
//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);
Response @ Bhavin: I did as per you, still not getting expected result. Please see below.
$(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);
});
}
Response @ Ziv Weissman MVC view (.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>
Solved: Refreshing ddlCountries dropdown after checking the option solved the problem.
var countries= $("#ddlCountries").multiselect('uncheckAll');
countries.children('option').each(function (){
var countryNameText = $(this).text();
if (countryNameText == "admin"){
$(this).attr('selected', 'selected');
}
});
$("#ddlCountries").multiselect("refresh");
Thanks all of you!
如果我放置两次 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
您必须刷新 multiSelect,以及为什么要在 selectForAdminRole 函数中添加 $(document).ready
$("#ddlCountries").find("option[value=" + ce + "]").prop("selected", true)
$("#ddlCountries").multiselect("refresh")
你可以使用这个
$("#ddlCountries").val("USA");
$("#ddlCountries").multiselect("refresh")