innerHTML replace does not reflect

一曲冷凌霜 提交于 2019-12-01 17:57:50

works fine for me in Firefox 3.5

Working Demo

EDIT: You must be using IE. You need to create a new option and add it to the element, or amend the text of the existing option

Working Demo - tested and working in FireFox and IE 7.

var foo = document.getElementById('foo');
var select = foo.getElementsByTagName('select');
select[0].options[0].text = ' two ';

Better to give an id to the select tag and use new option to add an item.

var sel =document.getElementById ( "selectElementID" );
var elOptNew = document.createElement('option');
elOptNew.text = 'Insert' + 2;
elOptNew.value = 'insert' + 2;
sel.options.add ( elOptNew );

If you need to replace an option

sel.options[indexNumber].text = 'text_to_assign';
sel.options[indexNumber].value = 'value_to_assign';

Setting the innerHTML of a select has a bug in IE. Not sure if they fixed this in 7 or 8 yet. It cuts off some of the text you give it and thus has malformed options. So you have to set the outerHTML which includes the select. Since outerHTML is IE only (or was) I normally use select.parentNode.innerHTML. And I assign the ID to the select. But since you are setting the innerHTML of the div, you are already covered. That was just an FYI.

I think your real problem is that replace is expecting a regular expression for the first argument. Or that the option element is not what you expect. IE can capitalize the option name and might be adding a selected attribute. You should alert the innerHTML to see what it is before attempting to do a string replace. Then I would use a valid regular expression to do the replace. You'll probably have to escape the slash with a backslash. I'm don't think you need to escape the angle brackets but I'm not 100% on that.

Also in my experience the DOM method (new Option) is slower. When you have to replace a large number of options it is significantly slower. If you just have to replace one or two, you can use the DOM method.

I guess you are using IE? The problem is that you have to use new Option(...) to populate SELECTs in IE. Like this:

document.getElementsByTagName('select')[0].options[0]=new Option(' two ');

Edit: There is a Microsoft Knowledgebase article about this bug: http://support.microsoft.com/kb/276228

A solution I worked out goes as follows

strHTML = "&nbsp;<option value=""1"">Text1</option>"
strHTML = strHTML + "<option value=""15"">Text2</option>"
strHTML = strHTML + "<option value=""17"">Text3</option>"


document.getElementById("id-selectbox").innerHTML=strHTML;
if(Browser=="Internet Explorer"){
    document.getElementById("id-selectbox").outerHTML=document.getElementById("id-selectbox").outerHTML;
}

The HTML string could be obtained by an AJAX request.

The result is that the browser will rewrite/render the selectbox with its newly added options.

Notice the: &nbsp preciding the first option code If you leave this out, the first option will loose its option tags and therefor will not be shown in the new added select-innerHTML

You're using replace which requires as the first argument a RegEx.

Try this:

document.getElementById('foo').innerHTML = document.getElementById('foo').innerHTML.replace(/<option> one <\/option>/, "<option> two </option>");

Does this work for you?

Check this

document.getElementById('foo').innerHTML.replace("<option> one </option>", "<option> two </option>") ;

if it is not working change the browser.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!