Вопрос по javascript – Получить выбранное значение в раскрывающемся списке с помощью JavaScript?

1495

Как получить выбранное значение из выпадающего списка, используя JavaScript?

Я попробовал методы ниже, но все они возвращают выбранный индекс вместо значения:

var as = document.form1.ddlViewBy.value;
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;
var value = document.getElementById("ddlViewBy").value;
Error: User Rate Limit Exceededfreakyjolly.com/demo/getDropDownValue_JavaScript_jQuery.html Code Spy

Ваш Ответ

23   ответа
3

e.options.selectedIndex

6

Error: User Rate Limit ExceededquerySelector.

E.g.

var myElement = document.getElementById('ddlViewBy');

var myValue = myElement.querySelector('[selected]').value;
2529

<select id="ddlViewBy">
  <option value="1">test1</option>
  <option value="2" selected="selected">test2</option>
  <option value="3">test3</option>
</select>

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;

strUser2test2

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].text;

strUsertest2

Error: User Rate Limit Exceededvar e = document.getElementById("ddlViewBy").value;
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededonchange :)
Error: User Rate Limit Exceeded
313

var e = document.getElementById("elementId");
var value = e.options[e.selectedIndex].value;
var text = e.options[e.selectedIndex].text;

$("#elementId :selected").text(); // The text content of the selected option
$("#elementId").val(); // The value of the selected option

: ():

// HTML
<select ng-model="selectItem" ng-options="item as item.text for item in items">
</select>
<p>Text: {{selectItem.text}}</p>
<p>Value: {{selectItem.value}}</p>

// JavaScript
$scope.items = [{
  value: 'item_1_id',
  text: 'Item 1'
}, {
  value: 'item_2_id',
  text: 'Item 2'
}];
element.options[e.selectedIndex].valueError: User Rate Limit Exceededelement.options[element.selectedIndex].value
Error: User Rate Limit Exceeded
Я, должно быть, делаю что-то не так, потому что, когда я пытаюсь это сделать, я получаю текст каждой опции в выпадающем списке.
13
<select id="Ultra" onchange="alert(this.value)"> 
 <option value="0">Select</option>
 <option value="8">text1</option>
 <option value="5">text2</option>
 <option value="4">text3</option>
</select>
6

$(‘select’).val();
12

$('#SelectBoxId option:selected').text(); for getting the text as listed

$('#SelectBoxId').val(); for getting the selected index value

Error: User Rate Limit Exceeded
-1

event.target.options[event.target.selectedIndex].dataset.name

Говоря о простоте, я думал оthisError: User Rate Limit Exceededevent.target
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
11

getElementByName()

my_nth_select_named_xmy_nth_text_input_named_y.

getElementByName:

var e = document.getElementByName("my_select_with_name_ddlViewBy");
var strUser = e.options[e.selectedIndex].value;
Error: User Rate Limit Exceeded
3

var selText = document.getElementById('elementId').options[document.getElementById('elementId').selectedIndex].text

var selText = (document.getElementById('elementId')).options[(document.getElementById('elementId')).selectedIndex].text;

2

Error: User Rate Limit Exceeded

var x = document.form1.list.value;

Error: User Rate Limit Exceededname="list"Error: User Rate Limit Exceededname="form1".

Error: User Rate Limit Exceededvar as = document.form1.ddlViewBy.value;Error: User Rate Limit Exceeded
8

idname

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Demo GetSelectOptionData</title>
</head>
<body>
    <form name="demoForm">
        <select name="demoSelect" onchange="showData()">
            <option value="zilch">Select:</option>
            <option value="A">Option 1</option>
            <option value="B">Option 2</option>
            <option value="C">Option 3</option>
        </select>
    </form>

    <p id="firstP">&nbsp;</p>
    <p id="secondP">&nbsp;</p>
    <p id="thirdP">&nbsp;</p>

    <script>
    function showData() {
        var theSelect = demoForm.demoSelect;
        var firstP = document.getElementById('firstP');
        var secondP = document.getElementById('secondP');
        var thirdP = document.getElementById('thirdP');
        firstP.innerHTML = ('This option\'s index number is: ' + theSelect.selectedIndex + ' (Javascript index numbers start at 0)');
        secondP.innerHTML = ('Its value is: ' + theSelect[theSelect.selectedIndex].value);
        thirdP.innerHTML = ('Its text is: ' + theSelect[theSelect.selectedIndex].text);
    }
     </script>
</body>
</html>

.

idname

52

Demo

 <select id="Ultra" onchange="run()">  <!--Call run() function-->
     <option value="0">Select</option>
     <option value="8">text1</option>
     <option value="5">text2</option>
     <option value="4">text3</option>
</select><br><br>
TextBox1<br>
<input type="text" id="srt" placeholder="get value on option select"><br>
TextBox2<br>
<input type="text" id="rtt"  placeholder="Write Something !" onkeyup="up()">

The following script is getting the value of the selected option and putting it in text box 1

<script>
    function run() {
        document.getElementById("srt").value = document.getElementById("Ultra").value;
    }
</script>

The following script is getting a value from a text box 2 and alerting with its value

<script>
    function up() {
        //if (document.getElementById("srt").value != "") {
            var dop = document.getElementById("srt").value;
        //}
        alert(dop);
    }
</script>

The following script is calling a function from a function

<script>
    function up() {
        var dop = document.getElementById("srt").value;
        pop(dop); // Calling function pop
    }

    function pop(val) {
        alert(val);
    }?
</script>
5

JavaScriptJQuery.

JavaScript:

var getValue = document.getElementById('ddlViewBy').selectedOptions[0].value;

alert (getValue); // This will output the value selected.

OR

var ddlViewBy = document.getElementById('ddlViewBy');

var value = ddlViewBy.options[ddlViewBy.selectedIndex].value;

var text = ddlViewBy.options[ddlViewBy.selectedIndex].text;

alert (value); // this will output the value selected

alert (text); // this will output the text of the value selected

JQuery:

$("#ddlViewBy:selected").text(); // Text of the selected value

$("#ddlViewBy").val(); // Outputs the value of the ID in 'ddlViewBy'
0

<select onChange="functionToCall(this.value);" id="ddlViewBy">
  <option value="value1">Text one</option>
  <option value="value2">Text two</option>
  <option value="value3">Text three</option>
  <option value="valueN">Text N</option>
</select> 
18

var e = document.getElementById("ddlViewBy"); 
var strUser = e.options(e.selectedIndex).value; 

var e = document.getElementById("ddlViewBy");    
var strUser = e.options[e.selectedIndex].value; 

163
var strUser = e.options[e.selectedIndex].value;

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

<select>
    <option value="hello">Hello World</option>
</select>

Index = 0 Value = hello Text = Hello World
Error: User Rate Limit Exceedede.target.options[e.target.selectedIndex].textError: User Rate Limit Exceeded
Да - сделайте значение параметра таким же, как оно есть. Проще - парень выше должен написать больше кода, чтобы компенсировать его начальную неопределенность.
4

var e = document.getElementById("ddlViewBy");
var val1 = e.options[e.selectedIndex].value;
var txt = e.options[e.selectedIndex].text;

document.write("<br />Selected option Value: "+ val1);
document.write("<br />Selected option Text: "+ txt);
<select id="ddlViewBy">
  <option value="1">test1</option>
  <option value="2">test2</option>
  <option value="3"  selected="selected">test3</option>
</select>

Error: User Rate Limit Exceeded
0

selectElement.value

document.getElementById("idOfSelect").value;

var res = document.querySelector('p');
var select = document.querySelector('select');
res.textContent = "Selected Value: "+select.value;
select.addEventListener("change", function(e){
  res.textContent = "Selected Value: "+select.value;
});
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Change value of the select to show the value.
<p>
</p>

2

document.getElementById('elementId').selectedOptions[0].value
29
var selectedValue = document.getElementById("ddlViewBy").value;
Error: User Rate Limit ExceededW3Schools.
1

document.getElementById('idselect').options.selectedIndex

1

<option>

Похожие вопросы