I want to get the value of the selected option from the selected box by using JQuery but it ain't working.
I have tried a million ways to do it. Trying to get its text instead, tried to get the value from multiple ways. To know what is coming in my jquery variable.
$(document).ready(function () { //First thing I tried var category = $('#category').val(); alert(Category); //Second thing I tried var category = $('#category').filter(":selected").val(); alert(Category); //Third thing I tried var category = $('#category').find(":selected").text(); alert(Category); //Fourth thing I tried var category = $('#category').find(":selected").val(); alert(Category); //Fourth thing I tried var category = document.getElementById('category').value; alert(Category); //Now directly taking the value of selected option by id alert($("#category :selected").attr('value')); //These are the code snippets I tried at different times so don't //think that I did this all a time which can obviously throw some //exception. });
<select class="form-control" id="cateogry" name="category"> <option value="0" disabled selected>Choose Project Category</option> <option value="active">Active Project</option> <option value="drafted">Drafted Project</option> </select>
All the options I have given above and tried all of them one by one and put an alert to see what is it getting and the alert said undefined. What can I do?
Html code
<select class="form-control" id="cateogry" name="category">
<option value="0" disabled selected>Choose Project Category</option>
<option value="active">Active Project</option>
<option value="drafted">Drafted Project</option>
</select>
jQuery code
$('#cateogry').change(function (){
let value=$(this).val();
alert(value);
});
Get for option selected
$( "#cateogry option:selected" ).val();
你的警报有类别(第一个字母大写),你的变量是类别(没有大写,你能确认拼写错误吗?也许你正在显示一个不存在的变量,你的一个尝试正在工作
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.