javascript/ razor

I am stuck & want to get selected the select option.

<select class="form-control" data-live-search="true" id="itemb">
<option data-tokens="All">All</option>

@foreach (var item in ViewBag.brandCategories)
{                                    
  <option>@item.brandName</option>
}
</select>

 //search by brand name.. $("#itemb").change(function () { var typp = document.getElementById("typ").value; var type = encodeURIComponent(typp); var brand = this.value; window.location.href = '/mobiles_tablets/item/?type=' + type + '&search=' + brand; }); 

In the above code just i get list in @foreach through @viewBag .but now I want when it return to the view it should get selected value.

please write js code?

Thanks.

This snippet of code will help you to get the selected value of select element. You can modify/change it through codepen check out the below link.

https://codepen.io/uicreation/pen/KZmXbw

 var el = document.getElementById('yourId'); el.onchange = function(){ console.log(this.selectedOptions[0].value); } 
 <select id="yourId"> <option>one</option> <option>two</option> <option>three</option> <option>four</option> <option>five</option> </select> 

or if you want to get selected option index use this.

var el = document.getElementById('yourId');
el.onchange = function(){
  console.log(this.selectedIndex);
}

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question How to get the value of selected select using localStorage How to get my JS script to run when select option is changed How to get the selected value in select? How to get object value when changed select option - Angular9 How to get selected value from select2 in vue js 2.0? Select value doesn't change when the selected option is changed How to get the selected value on select tag using JavaScript? How get selected value dynamically from a multiple select using MaterializeCss How to get Selected Text from select2 when using <input> Dynamically get changed select value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM