首页 > 解决方案 > Jquery动态添加行的复选框总是返回false

问题描述

我正在使用 jquery 在下拉更改事件上动态添加行。行包含复选框、TextArea 和 Dropdown。所有控件都与模型的属性绑定。Checkbox 还绑定了 Model 的 bool 属性。在提交表单时,控制器接收模型,其他控件(如 TextArea 和下拉列表)在表单中输入了值,但尽管选中了复选框,但复选框属性始终为 false。

这就是 HtmlHelper 类用于绑定模型的方式。

<tr>
    <td>
        @Html.CheckBoxFor(model => model.FunctionalRequirementsList[i].isSelected, new { @class = "checkbox" })
        @Html.HiddenFor(model => model.FunctionalRequirementsList[i].FunctionalRequirementId)
    </td>
    <td>                                        
        @Html.TextAreaFor(model => model.FunctionalRequirementsList[i].FunctionalRequirement, 5, 80, new { @class = "text  form-control col-md-10", style = "background-color:#fcf999;max-width:100%;height:50px;", ReadOnly = "true" })
    </td>
    <td>
        @Html.DropDownListFor(model => model.FunctionalRequirementsList[i].DefaultPriorityId, new SelectList(Model.FunctionalRequirementsList[i].ApllicabilityOptions, "Value", "Text", Model.FunctionalRequirementsList[i].DefaultPriorityId), new { @class = "form-control col-md-1" })
    </td>
</tr>

动态加法的jquery代码:

var oldFRSelected_Id = $trlast.find("input[type='checkbox'][id*='isSelected']").attr('id')
    var oldFRSelected_Id_Slice = oldFRSelected_Id.split('_'); 
    var newFRSelected_Id = oldFRSelected_Id_Slice[0] + "_" + (i + 1) + "__" + oldFRSelected_Id_Slice[3] 
    var newFRSelected_name = oldFRSelected_Id_Slice[0] + "[" + (i + 1) + "]." + oldFRSelected_Id_Slice[3]

    $trclone.find("input[type='checkbox'][id*='isSelected']").attr('id', newFRSelected_Id);
    $trclone.find("input[type='checkbox'][id*='isSelected']").attr('name', newFRSelected_name); 

标签: jqueryasp.net-mvcrazor

解决方案


推荐阅读