javascript 如何使用jquery或javascript设置带有选定下拉选项文本的innerhtml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27033122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:51:18  来源:igfitidea点击:

How to set innerhtml with selected dropdown option text using jquery or javascript?

javascriptjqueryhtml

提问by user2787474

I want to change the inner text of a anchor tag with the selected drop down option text

我想使用选定的下拉选项文本更改锚标记的内部文本

Here is my code of dropdown list whatever option is selected in this drop down list i want to update that option's text in another anchor tag

这是我的下拉列表代码 无论在此下拉列表中选择什么选项,我都想在另一个锚标记中更新该选项的文本

<span style="white-space: nowrap;" class="" id="shopperlanguage_fs">
<select class="input" id="shopperlanguage" name="shopperlanguage">
<option value="ja_JP" class="japImg">japanees</option>
<option selected="" value="en" class="engImg">English (International)</option>/* whatever option is selected like this i want to get this text  */
</select>
</span>

I want to update anchor tag class="dropdown-open" innerhtml with above mentioned dropdown
list selected text

我想用上面提到的下拉
列表选择文本更新锚标记 class="dropdown-open"innerhtml

<div class="top-lang clearfix">
<div class="cat-select">
<a href="#" data-dropdown="#dropdown-2" class="dropdown-open"><img src="/site/images 
/lang_05.jpg"> English</a>    /* this is the anchor tag which innerhtml i want to change with selected dropdown option text */
</div>
</div>

Suppose if in dropdown list selected option having text japanees then i want to change the anchor tag innerhtml text english to japanees.

假设如果在下拉列表中选择了包含文本 japanees 的选项,那么我想将锚标签innerhtml text english 更改为japanees。

this is my code which i tried but its not working

这是我尝试过但不起作用的代码

<script  type="text/javascript">
var e = document.getElementById("shopperlanguage"); 
var strUsertext = e.options[e.selectedIndex].text;
var langValue =$(".dropdown-open").text()
langValue.innerHTML= strUsertext;
</script>

回答by PeterKA

I added <span class="text"></span>to your markup so it would be easier to select the target area:

我添加<span class="text"></span>到您的标记中,因此选择目标区域会更容易:

$('#shopperlanguage').on('change', function() {
  $('.cat-select a span.text').text( $('option:selected',this).text() );
})
.change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span style="white-space: nowrap;" class="" id="shopperlanguage_fs">
<select class="input" id="shopperlanguage" name="shopperlanguage">
<option value="ja_JP" class="japImg">japanees</option>
<option selected="" value="en" class="engImg">English (International)</option>
</select>
</span>
<div class="top-lang clearfix">
<div class="cat-select">
<a href="#" data-dropdown="#dropdown-2" class="dropdown-open"><img src="/site/images 
/lang_05.jpg"> <span class="text">English</span></a>
</div>
</div>

回答by Priyank

Just try this.. :)

试试这个.. :)

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js">     </script>
<span style="white-space: nowrap;" class="" id="shopperlanguage_fs">
<select class="input" id="shopperlanguage" name="shopperlanguage">
<option value="ja_JP" class="japImg">japanees</option>
<option selected="" value="en" class="engImg">English (International)</option>/* whatever   option is selected like this i want to get this text  */
</select>
</span>
<div class="top-lang clearfix">
<div class="cat-select">
<a href="#" data-dropdown="#dropdown-2" class="dropdown-open">
<img src="/site/images/lang_05.jpg"> English</a>    /* this is the anchor tag which innerhtml i want to change with   selected dropdown option text */
</div>
</div>
<script  type="text/javascript">
$('#shopperlanguage').change(function(){
$('.dropdown-open').text($(this).find('option:selected').text());
});
</script>

回答by Balachandran

try in javaScript

在 JavaScript 中尝试

var e = document.getElementById("shopperlanguage");
var strUsertext = e.options[e.selectedIndex].text;
document.getElementsByClassName("dropdown-open")[0].childNodes[1].nodeValue = strUsertext;

DEMO

演示

use On change Jquery with javascript

使用 javascript 更改 Jquery

$('#shopperlanguage').on('change', function () {
    $(".dropdown-open")[0].childNodes[1].nodeValue = this.options[this.selectedIndex].text;
}).change();

DEMO

演示

回答by Shailesh Katarmal

<select name="abc" id="abc" onchange="chng()">
<option value=""></option>
<option value="English">English</option>
<option value="Japanese">Japanese</option>
</select>


<a href="#" id="atag">English</a>

<script>
function chng()
{
   var s= document.getElementById('abc').value;
   document.getElementById('atag').innerHTML = s;
}
</script>

This is working Demo. Please try it.

这是工作演示。请尝试一下。