javascript this.value 在 Firefox 上工作,但不在 Internet Explorer 上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3264630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
this.value is working on Firefox, but not in Internet Explorer?
提问by bahamut100
I have an onChangeevent on a select.
我有一个onChange关于select.
When I do an alert(this.value), it's working on Firefox, but not on Internet Explorer. Why not?
当我执行 . 时alert(this.value),它可以在 Firefox 上运行,但不能在 Internet Explorer 上运行。为什么不?
This is the code:
这是代码:
<select onchange="new Ajax.Updater('fiches', '/~project/index.php/folder/fiche', {asynchronous:true, evalScripts:true, parameters:'fiche=' + this.value});" class="input" id="fiche" name="fiche">
<option value="0">Choisir ma fiche</option>
<option value="1">Vous etes salariés</option>
<option value="2">Sans emploi</option>
</select>
回答by nacmartin
I have had similar problems in the past with IE and selects. The value property of select items is a pain in IE. Usually the solution is to care about the selected option (not the select element) and access to its text attribute.
我过去在使用 IE 和选择时遇到过类似的问题。选择项的 value 属性是 IE 中的一个痛点。通常的解决方案是关心选定的选项(而不是选择元素)并访问其文本属性。
I'll do this in jQuery to access the text selected:
我将在 jQuery 中执行此操作以访问所选文本:
$("#my_select_item option:selected").text()
So, in raw JavaScript it should be something like:
所以,在原始 JavaScript 中,它应该是这样的:
document.getElementById("myselect").options[document.getElementById("myselect").selectedIndex)].text
or
或者
document.getElementById("myselect").options[document.getElementById("myselect").selectedIndex)].value
That is the general idea to make selects + JS happen in IE.
这就是在 IE 中进行选择 + JS 的总体思路。
回答by Douwe Maan
Try this.options[this.selectedIndex].value.
试试this.options[this.selectedIndex].value。
回答by Jeremy Kauffman
It looks like you're using Prototype. Try calling $F(this)rather than this.value.
看起来您正在使用 Prototype。尝试调用$F(this)而不是this.value。

