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.