【问题标题】:How do I add chosen values of radio buttons by group names in c#?如何在 c# 中按组名添加单选按钮的选定值?
【发布时间】:2016-06-23 14:58:06
【问题描述】:

我有一个包含 47 组单选按钮的调查。每组有 4 个单选按钮。只能选择一个。我想将选择的值相加并产生一个总和。我已经设置了单选按钮的值。我只需要帮助选择值并将它们相加。

我以前从未这样做过,并且希望在如何做到这一点的大方向上得到一个想法或帮助。请。谢谢你

下面的示例代码

<div class="questionDiv" id="Question1"> <!--This ID will need to change for each question-->
  <div class="questionTextDiv">
    <p>1. how hard are you finding this.</p>
  </div> <!--End of questionTextDiv-->
  <div class="radioButtonDiv">
    <input type="radio" id="radio1" name="group1" value="0" /> <!--The group number will also need to change to the same number as questionID-->
    <label for="radio1">Never</label>
    <input type="radio" id="radio2" name="group1" value="1" />
    <label for="radio2">Sometimes</label>
    <input type="radio" id="radio3" name="group1" value="2" />
    <label for="radio3">Often</label>
    <input type="radio" id="radio4" name="group1" value="3" />
    <label for="radio4">Always</label>
  </div> <!--End of radioButtonDiv-->
</div> <!--End of questionDiv-->
<hr />
<div class="questionDiv" id="Question2"> <!--This ID will need to change for each question-->
  <div class="questionTextDiv">
    <p>2. when do you get hungry.</p>
  </div> <!--End of questionTextDiv-->
  <div class="radioButtonDiv">
    <input type="radio" id="radio5" name="group2" value="0" />     <!--The group number will also need to change to the same number as questionID-->
    <label for="radio5">Never</label>
    <input type="radio" id="radio6" name="group2" value="1" />
    <label for="radio6">Sometimes</label>
    <input type="radio" id="radio7" name="group2" value="2" />
    <label for="radio7">Often</label>
    <input type="radio" id="radio8" name="group2" value="3" />
    <label for="radio8">Always</label>
  </div> <!--End of radioButtonDiv-->
</div> <!--End of questionDiv-->

【问题讨论】:

  • c# 是怎么回事?
  • @MoshFeu 我不知道如何添加这些分组单选按钮的值
  • add the values 去哪里?我可以向您展示如何显示每个组的选定值。然后,您可以通过ajax 或其他方式将其发送到服务器。
  • @MoshFeu 听起来不错,谢谢

标签: javascript c# html asp.net


【解决方案1】:

您可以使用serialize 函数来实现。

将一组表单元素编码为字符串以供提交。

$('button').click(function() {
  var values = $('.question-wrapper input').serialize();
  alert(values);
  // then send it via ajax like
  $.ajax({
    url: 'your_handler',
    data: values,
    method: 'post'
    // so on
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="question-wrapper">
  <div class="questionDiv" id="Question1"> <!--This ID will need to change for each question-->
    <div class="questionTextDiv">
      <p>1. how hard are you finding this.</p>
    </div> <!--End of questionTextDiv-->
    <div class="radioButtonDiv">
      <input type="radio" id="radio1" name="group1" value="0" /> <!--The group number will also need to change to the same number as questionID-->
      <label for="radio1">Never</label>
      <input type="radio" id="radio2" name="group1" value="1" />
      <label for="radio2">Sometimes</label>
      <input type="radio" id="radio3" name="group1" value="2" />
      <label for="radio3">Often</label>
      <input type="radio" id="radio4" name="group1" value="3" />
      <label for="radio4">Always</label>
    </div> <!--End of radioButtonDiv-->
  </div> <!--End of questionDiv-->
  <hr />
  <div class="questionDiv" id="Question2"> <!--This ID will need to change for each question-->
    <div class="questionTextDiv">
      <p>2. when do you get hungry.</p>
    </div> <!--End of questionTextDiv-->
    <div class="radioButtonDiv">
      <input type="radio" id="radio5" name="group2" value="0" />     <!--The group number will also need to change to the same number as questionID-->
      <label for="radio5">Never</label>
      <input type="radio" id="radio6" name="group2" value="1" />
      <label for="radio6">Sometimes</label>
      <input type="radio" id="radio7" name="group2" value="2" />
      <label for="radio7">Often</label>
      <input type="radio" id="radio8" name="group2" value="3" />
      <label for="radio8">Always</label>
    </div> <!--End of radioButtonDiv-->
  </div> <!--End of questionDiv-->
  <button>Show Values</button>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2018-06-01
    • 2021-04-14
    相关资源
    最近更新 更多