InnerHTML IE 8无法正常工作?重置表单 [英] InnerHTML IE 8 doesn't work properly? Resetting form

查看:175
本文介绍了InnerHTML IE 8无法正常工作?重置表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,这可以在FF和Chrome中使用,但由于某些原因,在IE 8中不起作用。我使用单选按钮清除表单的一部分..该部分是选择框,但我不想将该区域留空 - 相反,我想将其重置为页面加载时的状态。目前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:

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?有没有解决这个问题的方法?

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!

推荐答案

试试这个:

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

然后创建一个新选项并将其推送到select的选项数组中。这些选项是一个棘手的问题,不像其他标记。

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;

这篇关于InnerHTML IE 8无法正常工作?重置表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆