<select> onChange事件后,在IE 10中React currentTarget.selectedOptions [0] .value失败

时间:2015-06-15 23:28:55

标签: internet-explorer reactjs

JSFiddle:https://jsfiddle.net/ypcrumble/kj2b8dk7/4/ 以下代码是我在IE 10上使用JSX在React 12.2应用程序中遇到的问题的简化版本。具体来说,该行 var postData = rsvp.currentTarget.selectedOptions [0] .value; 在IE中失败。它适用于Chrome和Firefox。首先,为什么这在IE10中失败,其次,如何调整代码使其工作? var Hello = React.createClass({     getInitialState:function(){         return {parsed:false};     },     _onRSVP:function(rsvp){         的console.log(RSVP);         var postData = rsvp.currentTarget.selectedOptions [0] .value;         this.setState({parsed:true});         警报(POSTDATA);     },     render:function(){         var parsing =&#34;解析工作&#34 ;;         回来(         &LT; DIV&GT;         &LT;选择             的onChange = {this._onRSVP}&GT;             &lt; option value =&#34; ----&#34;&gt; ----&lt; / option&gt;             &lt; option value =&#34;是&#34;&gt;是&lt; / option&gt;             &lt; option value =&#34; No&#34;&gt; No&lt; / option&gt;         &LT; /选择&GT;         &LT; p为H.         {this.state.parsed?         {}解析         : 空值}         &LT; / p为H.         &LT; / DIV&GT;         );     } }); React.render(&lt; Hello name =&#34; World&#34; /&gt ;, document.getElementById(&#39; container&#39;));

1 个答案:

答案 0 :(得分:3)

将此行var postData = rsvp.currentTarget.selectedOptions[0].value;更改为:

var postData = rsvp.nativeEvent.srcElement.value;

DEMO:has a section about Owin startup

编辑:通常你可以使用它来获取本机DOM事件的目标:

function getTarget(e) {
    var targ;

    if (!e) {
        e = window.event;
    }

    if (e.target) {
        targ = e.target;
    } else if (e.srcElement) {
        targ = e.srcElement;
    }

    if (targ.nodeType == 3) { // defeat Safari bug
        targ = targ.parentNode;
    }

    return targ;
}

然后你也可以改变上面一行:

var postData = getTarget(rsvp.nativeEvent).value;

fiddle