简体   繁体   English

如何从另一个位置的选择菜单显示选项标题

[英]How do I show option title from select menu in another spot

I am trying to get the title attribute from a selected Option in a Select menu. 我正在尝试从“选择”菜单中的选定“选项”中获取标题属性。 I have spent over a day on this and I am seeking help. 我已经花了整整一天的时间,正在寻求帮助。 I found a couple of ways to use the value attribute but now I need to use the title attribute somewhere else on the page. 我发现了几种使用value属性的方法,但是现在我需要在页面上的其他地方使用title属性。

Here is my current attempt although I have been through many iterations. 这是我目前的尝试,尽管我经历了很多次迭代。 I have listed two possible scripts although only one will eventually be used. 我列出了两个可能的脚本,尽管最终只会使用其中一个。 The first possible script might include Jquery and if it does and it can be used here, can you translate it into Javascript, as I am not good at either? 第一个可能的脚本可能包括Jquery,如果可以的话,可以在这里使用它,因为我也不擅长将它翻译成Javascript? Do I have the elements in the correct order? 我的元素顺序正确吗?

<select id="amount_id" onclick="function()">
<option value="" disabled="disabled">Amount</option> 
  <option value="0" title="None">0</option>
  <option value="1" title="One Quarter">1/4</option>
  <option value="2" title="One Half">1/2</option>
  <option value="3" title="Three Quarters">3/4</option>
  <option value="4" title="All">100%</option>
</select>
<textarea id="displayTitle"></textarea>
<script>
$(document).ready(function(){
    $(document).on('click','#amount_id',function(){
       var result = $("option:selected",this).attr('title');
       $("#displayTitle").text(result);
    });
});
</script>

OR 要么

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

Thank you. 谢谢。

Here is a JavaScript alternative of the first code : 这是第一个代码的JavaScript替代方法:

 <select id="amount_id"> <!-- removed onclick --> <option value="" disabled>Amount</option> <option value="0" title="None">0</option> <option value="1" title="One Quarter">1/4</option> <option value="2" title="One Half">1/2</option> <option value="3" title="Three Quarters">3/4</option> <option value="4" title="All">100%</option> </select> <textarea id="displayTitle"></textarea> <script> document.getElementById("amount_id").addEventListener('change',function(){ var eTitle = this.options[this.selectedIndex].getAttribute('title'); document.getElementById("displayTitle").value = eTitle; }); </script> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM