简体   繁体   English

如何从选择框中获取值?

[英]How can I get a value from the select box?

I want to get the value of the selected option from the selected box by using JQuery but it ain't working.我想通过使用 JQuery 从所选框中获取所选选项的值,但它不起作用。

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.要知道我的 jquery 变量中有什么。

 $(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 html代码



<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 jQuery 代码

  $('#cateogry').change(function (){

    let value=$(this).val();
    alert(value);

   });

Get for option selected获取选择的选项

$( "#cateogry option:selected" ).val();

你的警报有类别(第一个字母大写),你的变量是类别(没有大写,你能确认拼写错误吗?也许你正在显示一个不存在的变量,你的一个尝试正在工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从同一页面上的选择框中获取值 - How can I get a value from a select box on the same page 如何从选择框中获取值并使用它来获取API的响应? - How can i get a value from a select box and use it to get a response from an API? 我可以从给定的选择框文本中获取选择框值 - Can I get Select box value from given select box text 如何从 select 盒子到表中获取价值? - How to get value from select box to table? 当所有 select 框都有一个名称时,我如何从 DOM 添加/删除 select 框,以便在提交时我从活动的 select 框中获取值? - How can I add/remove select boxes from DOM when all select boxes have one name so that upon Submit I get value from active select box? 如何从JavaScript的选择框中获取价值? - How can i get value from selection box in javascript? 如何获取多个复选框的值并使用复选框中的值执行增量 - how can i get value of multiple checked box and perform increment using value from checked box 在单击之前,我无法获得选择框值 - I can't get a select box value until is clicked 如何从iframe内部的选择框中获取所选值,并将其显示在外部? - How do I get the selected value from a select box, from inside an Iframe, and display it on the outside? 更改复选框并更新选择框后,如何获取更新行的所有值? - How can i get all value of updated row when checkbox is changed and select box is updated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM