Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript获取所选文本而不是值_Javascript_Html - Fatal编程技术网

javascript获取所选文本而不是值

javascript获取所选文本而不是值,javascript,html,Javascript,Html,是否可以在select中获取所选文本而不是其值 <select id="animals"> <option value="mammals">Elephant</option> <option value="reptile">Snakes</option> <option value="mammals">Bear</option> <option value="birds">Os

是否可以在
select
中获取所选文本而不是其值

<select id="animals">
   <option value="mammals">Elephant</option>
   <option value="reptile">Snakes</option>
   <option value="mammals">Bear</option>
   <option value="birds">Ostrich</option>
</select>

大象
蛇
熊
鸵鸟
我想知道被选中的动物的类型

感谢您的帮助。

jQuery:

$("#selection option:selected").text();
使用纯Javascript:

this.options[this.selectedIndex].innerText
试试这个jQuery:

$("#selection option:selected").text();
function getSelectedText(elementId) {
    var elt = document.getElementById(elementId);

    if (elt.selectedIndex == -1)
        return null;

    return elt.options[elt.selectedIndex].text;
}

var text = getSelectedText('animals');
使用纯Javascript:

this.options[this.selectedIndex].innerText
试试这个

function getSelectedText(elementId) {
    var elt = document.getElementById(elementId);

    if (elt.selectedIndex == -1)
        return null;

    return elt.options[elt.selectedIndex].text;
}

var text = getSelectedText('animals');
使用,返回文档中与指定选择器组匹配的第一个元素

console.log(document.querySelector(“#动物选项:选中”).textContent)

大象
蛇
熊
鸵鸟
使用,返回文档中与指定选择器组匹配的第一个元素

console.log(document.querySelector(“#动物选项:选中”).textContent)

大象
蛇
熊
鸵鸟

可以使用javascript selectedIndex和text方法来完成


在下拉列表中选择一个选项并显示该选项

大象 蛇 熊 鸵鸟

函数myFunction(){ var x=document.getElementById(“mySelect”); var i=x.选择的指数; document.getElementById(“demo”).innerHTML=x.options[i].text; }
可以使用javascript selectedIndex和text方法来完成


在下拉列表中选择一个选项并显示该选项

大象 蛇 熊 鸵鸟

函数myFunction(){ var x=document.getElementById(“mySelect”); var i=x.选择的指数; document.getElementById(“demo”).innerHTML=x.options[i].text; }
我遇到了一个类似的问题,我想将所选文本(而不是值)输出到文本区域框中。我是这样解决的:

     <label>Objectives :</label>
<select name="objectives" class="objectives" onchange="getObjectives(this);">
<option selected="selected">--Select Objectives--</option>
<option selected="selected">--One--</option>
<option selected="selected">--Two--</option>
</select>

<textarea rows="4" cols="30" id="objid"></textarea>

<script>
function getObjectives(sel) {
// Select option returns text instead of value of option
document.getElementById ("animals").value = sel.options[sel.selectedIndex].text;

}

   </script>
目标:
--选择目标--
--一个--
--两个--
功能目标(sel){
//选择选项返回文本而不是选项的值
document.getElementById(“动物”).value=sel.options[sel.selectedIndex].text;
}

我遇到了一个类似的问题,我想将所选文本(而不是值)输出到文本区域框中。我是这样解决的:

     <label>Objectives :</label>
<select name="objectives" class="objectives" onchange="getObjectives(this);">
<option selected="selected">--Select Objectives--</option>
<option selected="selected">--One--</option>
<option selected="selected">--Two--</option>
</select>

<textarea rows="4" cols="30" id="objid"></textarea>

<script>
function getObjectives(sel) {
// Select option returns text instead of value of option
document.getElementById ("animals").value = sel.options[sel.selectedIndex].text;

}

   </script>
目标:
--选择目标--
--一个--
--两个--
功能目标(sel){
//选择选项返回文本而不是选项的值
document.getElementById(“动物”).value=sel.options[sel.selectedIndex].text;
}

你的javascript在哪里?你的javascript在哪里?你在哪里看到了
jQuery
标记?这可能会对你有所帮助。虽然我很感激你只是想提供帮助,但请坚持使用原始帖子中指定的技术/标记。虽然您的解决方案很可能是正确的,但它需要添加jQuery库。您在哪里看到了
jQuery
taged?这可能会对您有所帮助。虽然我很感激您只是想提供帮助,但请坚持使用原始帖子中指定的技术/标记。虽然您的解决方案可能非常正确,但它需要添加jQuery库。