简体   繁体   English

在 html 中显示 select 选项的值

[英]Show value of select option in html

I have a list with the我有一个清单tag, the item values are obtained through an online spreadsheet.标签,项目值是通过在线电子表格获得的。 When selecting an option, the value does not appear in the html in real time.选择选项时,该值不会实时出现在 html 中。 can anybody help me?有谁能够帮我? I'm trying to solve it, but I can't find the error.我正在尝试解决它,但我找不到错误。

CODEPEN LINK EXAMPLE CODEPEN 链接示例

 $(document).ready(function() { var sheetURL = "https://spreadsheets.google.com/feeds/list/1J0qyxUCCkvcFGZ6EIy9nDOgEuTlpI5ICVG3ZsBd_H-I/1/public/values?alt=json"; $.getJSON(sheetURL, function(data) { var entryData = data.feed.entry; console.log(data); jQuery.each(entryData, function() { $("#divTax").append( '<option hidden="hidden" selected="selected" value="0">CHOISE</option><option value="' + this.gsx$values.$t + '">' + this.gsx$names.$t + '&nbsp;&nbsp;&nbsp;$' + this.gsx$values.$t + '</option>' ); }); }); }); var totalWithTax = $('#divTax:selected').val(); $('.total').html(totalWithTax);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <select style="width: 80%;" id="divTax"></select> <br> Total:<div class="total"></div>

You to put the code that displays the selection in an event listener.您将显示选择的代码放在事件侦听器中。

 $(document).ready(function() { var sheetURL = "https://spreadsheets.google.com/feeds/list/1J0qyxUCCkvcFGZ6EIy9nDOgEuTlpI5ICVG3ZsBd_H-I/1/public/values?alt=json"; $.getJSON(sheetURL, function(data) { var entryData = data.feed.entry; jQuery.each(entryData, function() { $("#divTax").append( '<option hidden="hidden" selected="selected" value="0">CHOISE</option><option value="' + this.gsx$values.$t + '">' + this.gsx$names.$t + '&nbsp;&nbsp;&nbsp;$' + this.gsx$values.$t + '</option>' ); }); }); $("#divTax").change(function() { var totalWithTax = $(this).val(); $('.total').text(totalWithTax); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <select style="width: 80%;" id="divTax"></select> <br> Total: <div class="total"></div>

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

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