简体   繁体   English

Mercury-如何设置选项元素的“ selected”属性?

[英]Mercury - How do I set an option element's 'selected' property?

How do I set the selected property of an option element with Mercury / virtual-dom ? 如何使用Mercury / virtual-dom设置option元素的selected属性?

I've tried the following, but no selected property appears on the <option> : 我已经尝试了以下操作,但是<option>上没有selected属性:

h('select', {
  name: 'selectedRole', 
}, [
  h('option', {value: 'user', selected: true}, 'User'),
])

Instead, it results in this: 相反,它导致以下结果:

<select name="selectedRole">
  <option value="user">User</option>
</select>

After some testing, it appears this is how it works with Mercury. 经过一些测试,看来这就是它与Mercury一起工作的方式。 When you set selected to true on an option , it does become selected even though the property isn't visible on the HTML element. 当您在某个option上将selected设置为true时,即使该属性在HTML元素上不可见,它也会被选中。

However, I can only make it work in a small demo, not in my full application. 但是,我只能使其在一个小型演示中运行,而不能在我的完整应用程序中运行。 Thus, there could be a bug in virtual-dom. 因此,虚拟域中可能存在错误。

    $("option[value='user']").attr('selected', true);

I have also had this problem with the html5 datalist tag's list attribute. html5 datalist标记的list属性也遇到了这个问题。 One work-around is to use javascript, or jquery, to dynamically assign this attribute. 一种解决方法是使用javascript或jquery动态分配此属性。

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

相关问题 如果选择了 select 标记中的选项,我如何创建元素 - How do I create an element if the option in select tag is selected 如何设置通过查看MVC URL选择的下拉选项 - How do I set dropdown option selected by looking at MVC URL 我如何在Dojo form.select选项中设置选中 - how do i set selected in Dojo form.select option Angular.js:如何初始化的ng模型 <select>元素是ng-selected选项的值? - Angular.js: How do I initialize the ng-model of a <select> element to be the ng-selected option's value? 如何让Mercury Editor重定向到新资源? - How do I get Mercury Editor to redirect to a new resource? React-Select:如何在选定值 onChange 上更新选定选项下拉列表的默认值? - React-Select: How do I update the selected option dropdown's defaultValue on selected value onChange? 如何从select元素上的指令访问所选选项的显示值 - How can I access a selected option's displayed value from a directive on the select element 如何在 JavaScript 中设置元素的 style.top 属性? - How do I set the style.top property of an element in JavaScript? 如何动态删除选定的选项 - How do I remove a selected option dynamically 如何将元素的属性传递给React中的函数? - How do I pass an element's property to a function in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM