我想使用选定的下拉选项文本更改 anchor 标记的内部文本
这是我的下拉列表代码,无论在此下拉列表中选择什么选项,我都想在另一个 anchor 标记中更新该选项的文本
<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>
我想用上面提到的下拉菜单更新 anchor 标记 class="dropdown-open"insidehtml
列出选定的文本
<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>
假设如果在下拉列表中选择的选项包含文本 japanees,那么我想将 anchor 标记 insidehtml 文本英语更改为 japanees。
这是我尝试过的代码,但它不起作用
<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>
最佳答案
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>
关于javascript - 如何使用jquery或javascript设置innerhtml与选定的下拉选项文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27033122/