jquery 当调用动作时,复选框的问题值不影响模型属性speakstuff?

mf98qq94  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(110)

我在asp.net MVC应用程序上工作。我面临未检测到复选框值的问题
将模型属性speakstuff的值从true更改为false或反向。
所以我需要if yes checked then true if no checked then false然后影响模型属性speakstuff上的这个值。
html脚本表示是或否复选框

<table style="border: 1px solid black;width:100%;margin-bottom:5px;">

            <tr>
                <td style="width: 50%; font-weight: bold; padding-top: 10px;">
                    @Html.Label("Did You Meet/Speak to Staff", htmlAttributes: new { @class = "control-label col-md-5" })
                    <div class="col-md-7">
                        <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="true" class="speak-stuff-checkbox" @(Model.SpeakStuff ? "checked" : "") />
                        Yes
                        &nbsp;&nbsp;
                        <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox" @(!Model.SpeakStuff ? "checked" : "") />
                        No
                    </div>
                </td>
</table>

我通过java脚本从表单中获取值

<script type="text/javascript">
        function submit() {
            var ResignationRequester = new Object();
            ResignationRequester.SpeakStuff = document.getElementById("SpeakStuff").value;
            }
            

</script>

 jQuery code for single  check


$(document).ready(function () {
    $('.speak-stuff-checkbox').on('change', function () {
        $('.speak-stuff-checkbox').not(this).prop('checked', false);
    });

});

using (Html.BeginForm("ApprovalIndex", "Resignation", new { id = Model.RequestNo }, FormMethod.Post, htmlAttributes: new { @style = "display:inline;" }))
        {
            

            <a onclick="submit();" class="btn btn-primary" style="min-width: 100px;margin-top:5px;
            margin-left: 1300px;"><i class="glyphicon glyphicon-ok"></i> Approve </a>
            
        }


[HttpPost]
      
        public async Task<ActionResult> ApprovalIndex(ResignationRequester REQ)
        {
        }

那么如何解决这个问题呢?

更新文章

表示SpeakStuff属性的模型

public class ResignationRequester
   {
       [Display(Name = "Employee No : ")]
       [Required]
       public int EmpID { get; set; }
       [Display(Name = "Request No")]
       public int RequestNo { get; set; }
       public bool SpeakStuff { get; set; }

   }
7vhp5slm

7vhp5slm1#

为SpeakStuff创建隐藏字段

@Html.HiddenFor(x => Model.SpeakStuff)

删除复选框中的SpeakSuff属性

<div class="col-md-7">
                        <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="true" class="speak-stuff-checkbox") />
                        Yes
                        &nbsp;&nbsp;
                        <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox") />
                        No
                    </div>

修改您的JavaScript以在选中时更新隐藏字段的值

$(document).ready(function () {
            $('.speak-stuff-checkbox').on('change', function () {
                $('.speak-stuff-checkbox').not(this).prop('checked', false);
               var res =  $(this).val();
                $('#Model_SpeakStuff').val($(this).val());

                //alert($('#test').val());
                
            });
        });

speakstuff值是从隐藏字段中导出的。

相关问题