具有默认值的下拉列表选项不在列表中

时间:2020-11-24 20:17:44

I am writing to ask is it possible to have a drop down list with a value pre-populated that is not in the actual list.

我写信是否可能有一个下拉列表,其中预先填充的值不在实际列表中。

Say I have a system x that has a value y. and they can only ever choose on the new system value a or b. I would still like to display value y until they open the drop down at which point they loose it and it never comes back.

假设我有一个值为y的系统x。并且他们只能选择新的系统值a或b。我仍然希望显示值y,直到他们打开下拉,此时他们松开它并且它永远不会回来。

2 个解决方案

#1


6  

<select>
  <option value="" selected="selected">show girl</option>
  <option value="1">one</option>
</select>

on click remove the option

点击删除该选项

$("select").click(function(){
$("option[selected='selected']",this).remove();
});

UPDATE:

as @fbfcn suggested in the comments it would be more appropriate to use .one

正如@fbfcn在评论中建议的那样使用.one更合适

$("select").one("click",function(e){
$("option[selected='selected']",this).remove();
});

#2


0  

You can put a div on the dropdownlist with your wanted default value, then hide it when the user selects a value from the selectlist.

您可以使用所需的默认值在下拉列表中放置div,然后在用户从选择列表中选择值时将其隐藏。

but fitst try this:

但最适合试试这个:

$("#yourdropdownlist").val("defaultvalue"); 

#1


6  

<select>
  <option value="" selected="selected">show girl</option>
  <option value="1">one</option>
</select>

on click remove the option

点击删除该选项

$("select").click(function(){
$("option[selected='selected']",this).remove();
});

UPDATE:

as @fbfcn suggested in the comments it would be more appropriate to use .one

正如@fbfcn在评论中建议的那样使用.one更合适

$("select").one("click",function(e){
$("option[selected='selected']",this).remove();
});

#2


0  

You can put a div on the dropdownlist with your wanted default value, then hide it when the user selects a value from the selectlist.

您可以使用所需的默认值在下拉列表中放置div,然后在用户从选择列表中选择值时将其隐藏。

but fitst try this:

但最适合试试这个:

$("#yourdropdownlist").val("defaultvalue");