简体   繁体   English

如何根据选定的单选按钮值传递单选按钮值?

[英]How can i pass radio button values based on the selected one?

I have the following radio button in my view.我的视图中有以下单选按钮。 How can I pass the value of the selected radio button and values in an array index less than the selected radio button?.如何传递所选单选按钮的值和数组索引中小于所选单选按钮的值?

 foreach (var record in Model.Months.Select(x=>$"{x.Substring(4,2)}/{x.Substring(0,4)}").OrderByDescending(x=>x))
    {
         <div class="radio col-12">
            <div class="row">
              <div class="col-5" style="">
                @Html.RadioButton("MonthsAvailable", record, true, new { id = record, @class = "m-r" })<label>@record</label>
               </div>
             </div>
          </div>
    }

HTML Generated Code HTML 生成代码

<div class="row">
   <label class="form-check-inline p-l-md m-b-sm m-r-md">
      <input class="hidden-up pos-abs monthsAvailable" id="082020" name="MonthsAvailable" type="radio" value="202008" checked="checked">
      <div class="check-icon-radio"><i></i></div>
      082020
   </label>
</div>
<div class="row">
   <label class="form-check-inline p-l-md m-b-sm m-r-md">
      <input class="hidden-up pos-abs monthsAvailable" id="092020" name="MonthsAvailable" type="radio" value="092020">
      <div class="check-icon-radio"><i></i></div>
      092020
   </label>
</div>

My Model我的 Model

 public class MonthsAvailable
   {
    public List<string> Months{ get; set; }       
   }

My Action Receives我的行动收到

public async Task<IActionResult> MonthsAvailable(List<string> monthsAvailable)
 {
 ...
 }

My radio button looks like as follows我的单选按钮如下所示

在此处输入图像描述

I am trying to accomplish我正在努力完成

When I select 062020 pass only 062020 to the controller, 
When I select 072020 pass 062020 and 072020, 
when I select 082020 pass 062020, 072020 and  082020
when I select 092020 pass 062020, 072020, 082020 and 092020 and etc



              

You can give some outer div to all your .row div.您可以为所有.row div 提供一些outer div。 Then, whenever submit button is clicked first get checked radio button value using this we will get the index() number of the div where radio is checked.然后,每当单击提交按钮时,首先使用它获取checked的单选按钮值,我们将获得选中单选的 div 的index()编号。 Lastly, use for-loop to get the value from other radio buttons and push them in array and pass same using ajax.最后,使用for-loop从其他单选按钮获取值并将它们推入array并使用 ajax 传递相同的值。

Demo code :演示代码

 $("#save").on("click", function() { var checked_value = []; //declare this var value = $("input[name=MonthsAvailable]:checked").val() //get value of checked checkboxs var lengths = $("input[value=" + value + "]").closest(".row").index() //get index console.log($("input[value=" + value + "]").closest(".row").index()) //loop for (var i = 0; i <= lengths; i++) { checked_value.push($(".outer.row:eq(" + i + ") input:radio").val()) //push value inside array } console.log(JSON.stringify(checked_value)) //ajax call $.ajax({ contentType: 'application/json; charset=utf-8', type: 'POST', url: 'yoururl', data: JSON.stringify(checked_value), success: function(data) { console.log("done") } }); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!--only add this outer div--> <div class="outer"> <div class="row"> <label class="form-check-inline pl-md mb-sm mr-md"> <input class="hidden-up pos-abs monthsAvailable" id="082020" name="MonthsAvailable" type="radio" value="202008" checked="checked"> <div class="check-icon-radio"><i></i></div> 202008 </label> </div> <div class="row"> <label class="form-check-inline pl-md mb-sm mr-md"> <input class="hidden-up pos-abs monthsAvailable" id="092020" name="MonthsAvailable" type="radio" value="092020"> <div class="check-icon-radio"><i></i></div> 092020 </label> </div> <div class="row"> <label class="form-check-inline pl-md mb-sm mr-md"> <input class="hidden-up pos-abs monthsAvailable" id="082020" name="MonthsAvailable" type="radio" value="2020018" checked="checked"> <div class="check-icon-radio"><i></i></div> 2020018 </label> </div> <div class="row"> <label class="form-check-inline pl-md mb-sm mr-md"> <input class="hidden-up pos-abs monthsAvailable" id="092020" name="MonthsAvailable" type="radio" value="0920202"> <div class="check-icon-radio"><i></i></div> 0920202 </label> </div> </div> <button type="button" id="save">Save</button>

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

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