简体   繁体   English

如何在选择标签上设置选择,而不是选项标签

[英]How to set selected on select tag, not option tag

I wonder if there is a way to set selected on select tag not option tag我想知道是否有办法在 select 标签上设置 selected 而不是 option 标签

I know this works我知道这有效

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="vw" selected>VW</option>
  <option value="audi">Audi</option>
</select>

But I want to know if there is a way using select tag.但我想知道是否有使用 select 标签的方法。

Something like就像是

<select selected="audi">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="vw">VW</option>
  <option value="audi">Audi</option>
</select>

I know second code does not work.我知道第二个代码不起作用。

I hope there is a way to work like first code, using select tag.我希望有一种方法可以像第一个代码一样使用 select 标签。

Thanks!谢谢!

It is not a default property of the select tag.它不是select标签的默认属性。
To realize this you should make your own function in javascript.要实现这一点,您应该在 javascript 中创建自己的函数。
You should also include Jquery for this.您还应该为此包含Jquery
This is total unachieveable in javascript.这在 javascript 中是完全无法实现的。 You can add data attributes to elements.您可以向元素添加data属性。

<select id="cars" data-selected="audi">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="vw">VW</option>
  <option value="audi">Audi</option>
</select>

In javascript you can read the value and set it to the option.在 javascript 中,您可以读取该值并将其设置为选项。

var selected = $("#cars").attr("data-id");
$('#cars option[value="'+ selected +'"]').prop('selected', true);

But anyway, why do you want this?但无论如何,你为什么要这个?
<select selected="audi"> would never work... <select selected="audi">永远不会工作......

The answer is: No.答案是不。

Although you could write some javascript that would set the selected attribute on an option based on a parameter in the select tag but you would then modify the DOM which results in your example #1.尽管您可以编写一些 javascript 来根据select标记中的参数在option上设置selected属性,但您随后会修改DOM ,从而生成示例 #1。

So no, there is no way in HTML5 to have a DOM like you're asking for.所以不,HTML5 没有办法像你要求的那样拥有一个DOM

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

相关问题 如何在选择标签中设置所选选项的颜色 - How to set the color of a selected option in a select tag 如何使用现有值设置为选择标签选择的选项值 - How set option value selected for select tag with existing values 使用jstl在现有选择标记上设置选定选项 - Set selected option on existing select tag with jstl 通过属性名称设置选择的选项(选择标签) - Set the selected option by attribute name (select tag) 标签选择 - 选择的选项 - Tag Select - selected option 如何使用 JavaScript\\JQuery 使用选择标记的选定选项的值设置隐藏输入标记的值? - How can I set the value of an hidden input tag with the value of the selected option of a select tag using JavaScript\JQuery? 如何设置 SELECT Tag 的默认选项以及如何获取所选选项的索引? - How to set the default option for SELECT Tag and how to get the Index of the selected option? 如何使用带有“选定的已禁用隐藏”选项的select_tag? - How to have a select_tag with an “selected disabled hidden” option? 如何通过选择 select 标签中的某个选项将 URL 获取到页面 - How to a get a URL to a page with a certain option from select tag selected 在HTML中选择标签,如何检查选择了哪个选项 - Select tag in HTML, how to check which option is selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM