【问题标题】:Show value of select option in html在 html 中显示选择选项的值
【发布时间】:2021-02-10 20:01:51
【问题描述】:

我有一个带有标签的列表,项目值是通过在线电子表格获得的。选择选项时,该值不会实时显示在 html 中。有谁能够帮助我?我正在尝试解决它,但我找不到错误。

CODEPEN LINK EXAMPLE

 $(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>

【问题讨论】:

  • 您似乎缺少像 $('select').on('change', function () 或 $('#divTax').on('change',函数()

标签: javascript html jquery google-sheets


【解决方案1】:

您可以将显示选择的代码放入事件侦听器中。

$(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>

【讨论】:

    最近更新 更多