我在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
<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; }
}
1条答案
按热度按时间7vhp5slm1#
为SpeakStuff创建隐藏字段
删除复选框中的SpeakSuff属性
修改您的JavaScript以在选中时更新隐藏字段的值
speakstuff值是从隐藏字段中导出的。