InnerHTML IE 8 无法正常工作?重置表单

发布于 2024-08-27 04:52:51 字数 686 浏览 4 评论 0原文

是的,这在 FF 和 Chrome 中有效,但由于某种原因在 IE 8 中不起作用。我使用单选按钮来清除表单的一部分..该部分是一个选择框,但我不想离开区域为空 - 相反,我想将其重置为页面加载时的状态。目前 IE8 只给我留下一个空的小选择框。

Html:

<select id="city_select" disabled="true" name="location_id" onchange="show_search_button();"><option selected>Select your city</option> </select>

Javascript:

document.getElementById('city_select').innerHTML = "<option selected>Select your city</option>";

我也尝试在javascript中使用location_id而不是city_select,但无济于事..innerText和innerContent也不起作用..虽然inner.HTML在IE8中适用于早期功能,但这并不是试图innerHTML成一种形式。有谁知道为什么这在 Chrome 和 FF 中有效,但在 IE8 中无效?有解决办法吗?任何帮助表示感谢!

Yeah this works in FF and Chrome, but for some reason wont work in IE 8. I'm using a radio button to clear a section of a form.. that section is a select box, but I don't want to leave the area empty - instead I want to reset it to what it was when the page loaded. At the moment IE8 is just leaving me with an empty small select box.

Html:

<select id="city_select" disabled="true" name="location_id" onchange="show_search_button();"><option selected>Select your city</option> </select>

Javascript:

document.getElementById('city_select').innerHTML = "<option selected>Select your city</option>";

I've also tried using location_id instead of city_select in the javascript but to no avail.. innerText and innerContent dont work either.. though the inner.HTML works in IE8 for an earlier function, but that isnt trying to innerHTML into a form. Does anybody know why this works in Chrome and FF but not IE8? and is there a solution to this? Any help appreciated thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吾性傲以野 2024-09-03 04:52:51

试试这个:

document.getElementById('city_select').options.length = 0;

然后创建一个新选项并将其推送到选择的选项数组中。这些选项有点棘手,其行为与其他标记不同。

编辑以显示如何创建选项:

var sel = document.getElementById('city_select').options.length = 0;
var opt = document.createElement('option');
opt.value = "Select Your City";
sel.options.push(opt);
sel.selectedIndex = 0;

Try this:

document.getElementById('city_select').options.length = 0;

Then create a new option and push it onto the options array of the select. The options are a tricky bit that don't behave like other markup.

Edited to show how to create an option:

var sel = document.getElementById('city_select').options.length = 0;
var opt = document.createElement('option');
opt.value = "Select Your City";
sel.options.push(opt);
sel.selectedIndex = 0;
少跟Wǒ拽 2024-09-03 04:52:51

应该有 4 种方法将新选项分配给选择元素。有些在某些场景下工作,有些在其他场景下工作。看这里 - 如何将选项添加到< SELECT>, 在 IE Windows Mobile 5

对我来说,Robusto 的解决方案由于三个原因而不起作用:

1) 第一行中的 sel 变量被分配了 document.getElementById( 'city_select').options.length = 0; 而不是简单地保留 select 元素(以便稍后在第 4 行和第 5 行中使用),然后删除下一行中的选项,如下所示:

var sel = document.getElementById('city_select');
sel.options.length = 0;

2) 第 4 行 < code>sel.options.push(opt)(或稍后建议的sel.options[0] = opt)抛出一个对象不支持此属性或方法 错误。而是使用这个:

sel.appendChild(opt);

3)除了为选项分配值之外,您还必须分配要显示的文本。你这样做:

opt.innerText = "Select Your City - displayed";

因此,总结整件事:

var sel = document.getElementById('city_select');
sel.options.length = 0;
var opt = document.createElement('option');
opt.value = "Select Your City";
opt.innerText = "Select Your City - displayed";
sel.appendChild(opt);
sel.selectedIndex = 0;

There are supposed to be 4 way of assigning new options to a select element. Some work in some scenarios and others work in others. Look here - How to Add options to <SELECT>, in IE Windows Mobile 5

For me, Robusto's solution didn't work due to three reasons:

1) sel variable in the first line is assigned document.getElementById('city_select').options.length = 0; instead of simply holding the select element (for later use in 4th and 5th line) and then deleting options in a next line, like this:

var sel = document.getElementById('city_select');
sel.options.length = 0;

2) the 4th line sel.options.push(opt) (or later suggested sel.options[0] = opt) throws an Object doesn't support this property or method error. Instead use this:

sel.appendChild(opt);

3) apart from assigning values to options you must also assign text to display. You do it like this:

opt.innerText = "Select Your City - displayed";

Therefore, to sum up the whole piece:

var sel = document.getElementById('city_select');
sel.options.length = 0;
var opt = document.createElement('option');
opt.value = "Select Your City";
opt.innerText = "Select Your City - displayed";
sel.appendChild(opt);
sel.selectedIndex = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文