我如何 select html select 中的一个选项,我知道它的值?
How can I select an option in an html select where I know the value?
在 jQuery 中,如果我有一个 ID 为 "parentId" 的 html select,我如何才能 select 一个我知道期权的价值?
这是我的代码:
function selectParentId(parentId)
{
$('#parentId').find('option').each(function(index,element){
if(element.value == parentId)
element.prop('selected', true);
});
}
我需要如何更改以上代码?
要select 一个使用值的项目,你可以试试这个:
$('#parentId option[value="yourvalue"]').prop("selected", 1);
因此您的函数可以重写为:
function selectDropdown(id, value)
{
$('#'+id+' option[value="'+value+'"]').prop("selected", 1);
}
所以你可以这样称呼它:
selectDropdown('theIdOfSelect', 'theValueOfSelect');
一个例子Here。
试试这个:
function selectParentId(id,value) {
$('#' + id).val(value)
}
在 jQuery 中,如果我有一个 ID 为 "parentId" 的 html select,我如何才能 select 一个我知道期权的价值?
这是我的代码:
function selectParentId(parentId)
{
$('#parentId').find('option').each(function(index,element){
if(element.value == parentId)
element.prop('selected', true);
});
}
我需要如何更改以上代码?
要select 一个使用值的项目,你可以试试这个:
$('#parentId option[value="yourvalue"]').prop("selected", 1);
因此您的函数可以重写为:
function selectDropdown(id, value)
{
$('#'+id+' option[value="'+value+'"]').prop("selected", 1);
}
所以你可以这样称呼它:
selectDropdown('theIdOfSelect', 'theValueOfSelect');
一个例子Here。
试试这个:
function selectParentId(id,value) {
$('#' + id).val(value)
}