原生is获取select 中 option 的 data 的值

首先给定select 一个 唯一id

<select className={'form-control'} id={'partner'} onClick={this.getPartnerList.bind(this)} onChange={this.setMultiPartner.bind(this)}>
    <option value=''>请选择</option>
    {partnerArr.length > 0 ? partnerArr.map((elem, idx) => {
    return <option key={idx} data-id={elem._id} value={elem.name}>{elem.name}</option>}) : ''} 
</select>

当选择不同的option 时 触发 onChange 方法

原生js获取点击的select 签这个对象:

const partnerSelect = document.getElementById("partner");

获取当前点击的option 的索引(两种渠道都可以)

①const selectedIndex = evt.currentTarget.selectedIndex  (需要传入当前点击对象)

②const selectedIndex = partnerSelect. selectedIndex  直接获取

此时就可以得到当前选中的option了

var selectValue = selectTest.options[selectIndex].value;  //获取当前选中的option的value值
var selectText = selectTest.options[selectIndex].text;//获取当前选中的option的text值

var selectDataset = selectTest.options[selectIndex].dataset;//获取当前选中的option的所有 的 data 的 值

猜你喜欢

转载自blog.csdn.net/zlzbt/article/details/102977838