【问题标题】:How to take values ​from several checkboxes if one checkbox is checked?如果选中一个复选框,如何从多个复选框中获取值?
【发布时间】:2019-03-04 18:01:57
【问题描述】:

我有一个复选框,一个在表中,一个在表外(选中所有)。表格外的复选框用于检查表格中每一行上的所有复选框,并取值复选框并将其输入文本框中。如果表外的复选框已被选中,我创建了一个函数来检查表中的所有复选框。问题是我无法检索所有复选框值并将其输入到文本框中。如果表外的复选框已被选中,如何取所有值复选框? 以下是我的看法

<div id="sectionTable">
    <input type="checkbox" name="check_all" onclick="paymentCheckedAll(this);" /><span>Check All</span>
</div>
<table class="table">
    <tr>
      <th>selected</th>
      <th>Name</th>                          
    </tr>
    @foreach (Learning.ViewModels.StudentViewModelitem in Model.StudentDetails)
    { 
      <tr>
        <td>
           @Html.CheckBoxFor(modelitem => item.IsChecked, new {@class = "idRow", @id = "test", @value="123" })           
        <td>
        <td>
           @Html.DisplayFor(modelitem => item.Name)
        </td>
      </tr>
    }
</table>
   @Html.TextBoxFor(model => model.ListSelectedIdPayment)

<script>
    $(document).ready(function () {
        $(document).on(' change', 'input[name="check_all"]', function () {
            $('.idRow').prop("checked", this.checked);
        });
    });
</script>

【问题讨论】:

  • 能否提供设置值的函数?
  • 我在checkbox里设置了静态值,值为123 @P.B.UDAY

标签: javascript


【解决方案1】:

如果你想要所有选中复选框的值:

$('.idRow').each(function(){

 if($(this).attr('checked')){
     //set value here by $(this).attr('value')
  }
});

【讨论】:

    猜你喜欢
    • 2021-11-21
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多