单选按钮返回列表中的最终值

时间:2020-06-23 14:41:01

标签: javascript html google-apps-script

对于JS(应用程序脚本)和HTML而言相对较新

请您帮忙-我有一个通常可以完美提交的表格(文本框,日期框),但是收音机始终在选择最终值。这是HTML(从模板站点获得的引导CSS)和JS,它们用于在末尾收集数据-

  <div class="col-md-6"> 
<label class="radio-inline" for="ST0">
  <input type="radio" name="STRating" id="ST0" value="1">
  1
</label> 
<label class="radio-inline" for="ST1">
  <input type="radio" name="STRating" id="ST1" value="2">
  2
</label> 
<label class="radio-inline" for="ST2">
  <input type="radio" name="STRating" id="ST2" value="3" checked="checked">
  3
</label> 
<label class="radio-inline" for="ST S">
  <input type="radio" name="STRating" id="ST3" value="4">
  4
</label> 
<label class="radio-inline" for="ST4">
  <input type="radio" name="STRating" id="ST4" value="5">
  5
</label>

还有Apps脚本

 function testing143(){
var form = document.getElementById("myForm").elements;
var obj ={};
for(var i = 0 ; i < form.length ; i++){
    var item = form.item(i);
    obj[item.name] = item.value;
}
google.script.run.withSuccessHandler(success).testing143(obj);

2 个答案:

答案 0 :(得分:2)

单选按钮的所有五个元素都具有相同的名称,因此您的代码将值重新分配给obj['STRating']的五倍,最后的赋值(5)是您所得到的。 而不是无条件分配

obj[item.name] = item.value;

您应该只为选中的按钮设置分配条件,例如:

if (item.checked) obj[item.name] = item.value;

通过这种方式,您仍然可以浏览所有五个按钮,但是只记住已选中的一个按钮的值。

答案 1 :(得分:1)

您正在使用命名数组,并且有五个具有相同名称“ STRating”的元素。最后一个循环会将其设置为“ 5”。

我认为您需要更喜欢所选值。更多信息:How to get the selected radio button’s value?

相关问题