简体   繁体   English

的HTML <option>标签 。如何选择一个值?

[英]HTML <option> Tag . how to select a value?

Can any one help me with this.. 谁能帮我这个..

i have an option box like this 我有一个像这样的选项框

<select name="facility_info_input[facility_type_id]" id="facility_type_id">
    <option value=1 >Room</option>
    <option value=2 >Equipment</option>
</select>

i want to select one of the above options automatically according to the variable value(before user filling/updating the form the option should be already selected), As an example i have a data variable(read from the database) and its value is 1 /2 , if its 1 can this "Room" option automatically selected, I have been trying to use selected="selected" attribute, but still I couldn't figure it out how to use it,is their a any other way? 我想根据变量值自动选择上述选项之一(在用户填写/更新表格之前,应该已经选择了该选项),例如,我有一个数据变量(从数据库中读取),其值为1 / 2,如果可以自动选择此“房间”选项的1,则我一直在尝试使用selected =“ selected”属性,但仍然不知道如何使用它,这是其他方式吗?

Thank you in advance.. 先感谢您..

Add selected="selected" to the option you want selected by default. 默认情况下,将selected="selected"添加到您要选择的选项中。 I am assuming you are doing this server side. 我假设您正在此服务器端。

<select name="facility_info_input[facility_type_id]" id="facility_type_id">
    <option value="1" >Room</option>
    <option selected="selected" value="2" >Equipment</option>
</select>

Also, you need to enclose your attributes in quotation marks. 另外,您需要将属性用引号引起来。 This may be why your code wasn't working. 这可能就是为什么您的代码无法正常工作的原因。

<option value="1" /> //good
<option value=1 /> //bad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM