如何在c#中按组名添加选定的单选按钮值?

时间:2016-03-09 09:52:00

标签: javascript c# html asp.net

我的调查包含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-->

1 个答案:

答案 0 :(得分:0)

您可以使用serialize函数完成此操作。

  

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

&#13;
&#13;
$('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
  });
});
&#13;
<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>
&#13;
&#13;
&#13;