简体   繁体   English

将选定和禁用的属性添加到IE 11中的选项标签

[英]Adding selected and disabled attributes to an option tag in IE 11

I'm trying to debug some code that doesn't display properly. 我正在尝试调试一些无法正常显示的代码。 The code should add a default option to an drop down list that is disabled from selection. 该代码应将一个默认选项添加到一个下拉列表中,该列表被选中。 What I get is that the list option is displayed but not selected by default. 我得到的是列表选项已显示,但默认情况下未选中。 After the page is loaded I inspect element on that list box and I am shown the following html for the first item of the list: 页面加载后,我检查该列表框上的元素,并为列表的第一项显示以下html:

<option value="" disabled="">

When I select edit as HTML the tag expands to 当我选择将HTML编辑为HTML时,标记会展开

<option value="" disabled="" selected="">

By simply creating a local html file using the tag with selected="" I can get the desired results but I'm not sure why it doesn't appear in the inspector until I select edit as HTML. 通过使用带有selected=""标记的标签简单地创建本地html文件,可以获得所需的结果,但是我不确定为什么直到选择HTML编辑后它才出现在检查器中。

Here is the rest of the code pertaining to this issue: 以下是与此问题有关的其余代码:

<div class='input-block smallish' id="inquiry-type">
  <span class='label'>
    Inquiry Type <span>(required)</span>
  </span>
  <div>
    <select class="default xl" id="ticket_custom1" name="ticket[custom1]">
    <option value="Problem Report">Problem Report</option>
    <option value="Usage or Technical Question">Usage or Technical Question</option>
    <option value="Enhancement Request">Enhancement Request</option>
    <option value="New Project Idea">New Project Idea</option>
    <option value="Other">Other</option></select>
  </div>
</div>

<script>
    ...
    // Add our default for lists
    $('.input-block:not(.disable-default) select').prepend('<option value="" disabled selected>-- Select one --</option>');
    ...
</script>

There are more scripts but I don't think they pertain to this error. 还有更多脚本,但我认为它们与该错误无关。 Also I get the desired results in Chrome. 另外,我在Chrome浏览器中获得了预期的结果。

There definitely does appear to be a difference between Internet Explorer and Chrome/Firefox. Internet Explorer和Chrome / Firefox之间肯定确实存在差异。 That being said, you can avoid this issue by explicitly setting the selectedIndex of the element itself: 话虽如此,您可以通过显式设置元素本身的selectedIndex来避免此问题:

$( "select" ).prop( "selectedIndex", 0 );

I'll file an Interop bug internally on this to have our team look into the issue and make a determination as to what the appropriate behavior should be. 我将在内部为此提交一个Interop错误,以使我们的团队调查该问题并确定应采取的适当行为。

Fiddle: http://jsfiddle.net/qtcs1kxe/1/ 小提琴: http : //jsfiddle.net/qtcs1kxe/1/
Feature Test: http://jsfiddle.net/jonathansampson/ub1yt4vv/show 功能测试: http : //jsfiddle.net/jonathansampson/ub1yt4vv/show

I guess IE11 doesn't notice the selected attribute when you're appending after rendering. 我猜想IE11在渲染后追加时不会注意到selected属性。

You'll need to select the option in your script. 您需要在脚本中选择选项。 Just add val('') to the end of your prepend. 只需在您的前缀末尾添加val('') Works for me. 为我工作。

$('.input-block:not(.disable-default) select').prepend('<option value="" disabled selected>-- Select one --</option>').val('');

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

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