需要使用 JavaScript 选择所选下拉列表的文本

我有一个包含值的下拉菜单。我有一个数组,其中包含与下拉值匹配的值列表。如果数组中存在下拉列表的文本选项的值,则它不应作为选项显示在下拉列表中。我被困在我应该使用的方法上。这是我到目前为止所拥有的。


超文本标记语言


Car Plates: 

<select title='car/id' id='car_x0020_Plate_x002f'>

  <option selected="selected" value="0">none</option>

  <option value="16">233-jj2</option>

  <option value="10">934-zxy</option>

  <option value="90">330-nbh</option>

  <option value="11">930-orj</option>

</select>

JavaScript


var hideOption = ['233-jj2', '330-nbh']


var e = document.querySelector([id^='car']);

var strUser = e.value;

                               

var e = document.getElementById("ddlViewBy");

var strUser = e.options[e.selectedIndex].text;


for (var x=0; x<hideOption.length; x++){

  if (hideOption[x] === strUser){

    //remove from dropdown

  }

}


函数式编程
浏览 121回答 4
4回答

HUH函数

我以非常简单的方式提出了你的想法,如果你有任何问题请告诉我var hideOption = ['233-jj2', '330-nbh'],&nbsp; &nbsp; select = document.getElementById("select");for (let i = 0; i < hideOption.length; i = i + 1) {&nbsp; &nbsp; for (let t = 1; t < select.options.length; t = t + 1) {&nbsp; &nbsp; &nbsp; &nbsp; if (hideOption[i] == select.options[t].textContent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select.options[t].remove();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}Car Plates:&nbsp;<select title='car/id' id='select'>&nbsp; <option selected="selected" value="0">none</option>&nbsp; <option value="16">233-jj2</option>&nbsp; <option value="10">934-zxy</option>&nbsp; <option value="90">330-nbh</option>&nbsp; <option value="11">930-orj</option></select>

HUX布斯

// 从下拉列表中删除 使用此代码从下拉列表中删除 e.removeChild(e.options[e.selectedIndex])您也可以使用 e.selectedOptions[0].remove()

30秒到达战场

var hideOption = ['233-jj2', '330-nbh']var e = document.querySelector("[id^='car']");var selTextArr = Array.from(e.options).map(option => option.text)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (var x=0; x<selTextArr.length; x++){&nbsp; if (hideOption.includes(selTextArr[x])){&nbsp; &nbsp; e.remove(x)&nbsp; }}Car Plates:&nbsp;<select title='car/id' id='car_x0020_Plate_x002f'>&nbsp; <option selected="selected" value="0">none</option>&nbsp; <option value="16">233-jj2</option>&nbsp; <option value="10">934-zxy</option>&nbsp; <option value="90">330-nbh</option>&nbsp; <option value="11">930-orj</option></select>

长风秋雁

var options = document.querySelector("[id^='car']").children;var hideOption = ['233-jj2', '330-nbh']for (var i = 0; i < options.length; i++){&nbsp; &nbsp; if(hideOption.indexOf(options[i].text) > -1){&nbsp; &nbsp; &nbsp; &nbsp; options[i].remove();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript