gpt4 book ai didi

jquery - 如果使用 JQuery 选中复选框,如何添加值?

转载 作者:行者123 更新时间:2023-12-01 08:40:06 24 4
gpt4 key购买 nike

如果选中该复选框,则在原始值中添加值 3;如果未选中,则减去 3 并保留原始值。

 <label>Cost of this report: $ @Session["ProductCost"]</label>

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{

<div>
@Html.CheckBoxFor(m => m.IsTrainers)
@Html.LabelFor(m => m.IsTrainers, "Include trainers in first Dam")
</div>
<div>
@Html.CheckBoxFor(m => m.IsSales)
@Html.LabelFor(m => m.IsSales, "Include Sales in first Dam")
</div>
<div>
@Html.CheckBoxFor(m => m.IsResearch)
@Html.LabelFor(m => m.IsResearch, "Research Style (all foals inc)")
</div>
<div>
@Html.CheckBoxFor(m => m.IsEngagement)
@Html.LabelFor(m => m.IsEngagement, "Include Race engagements in first Dam")
</div>

}

此复选框的模型:

public class ClsHome
{
public bool IsTrainers { get; set; }
public bool IsSales { get; set; }
public bool IsResearch { get; set; }
public bool IsEngagement { get; set; }
public bool IsFamilyRunners { get; set; }

}

任何人请指导我如何执行此操作。提前谢谢您。

最佳答案

尝试以下方法

<label>Cost of this report: $ <span id="spProductCost">@Session["ProductCost"]</span></label>
<input type="hidden" id="productCost" value="@Session["ProductCost"]" />

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<div>
@Html.CheckBoxFor(m => m.IsTrainers, new { onchange = "changeProductCost(this)" })
@Html.LabelFor(m => m.IsTrainers, "Include trainers in first Dam")
</div>
<div>
@Html.CheckBoxFor(m => m.IsSales, new { onchange = "changeProductCost(this)" })
@Html.LabelFor(m => m.IsSales, "Include Sales in first Dam")
</div>
<div>
@Html.CheckBoxFor(m => m.IsResearch, new { onchange = "changeProductCost(this)" })
@Html.LabelFor(m => m.IsResearch, "Research Style (all foals inc)")
</div>
<div>
@Html.CheckBoxFor(m => m.IsEngagement, new { onchange = "changeProductCost(this)" })
@Html.LabelFor(m => m.IsEngagement, "Include Race engagements in first Dam")
</div>
}

并添加以下 JavaScript

<script>
var original = parseInt('@Session["ProductCost"]');
function changeProductCost(chk){
if ($(chk).prop("checked")) {
original = original + 3;
}
else {
original = original - 3;
}
$("#productCost").val(original);
$("#spProductCost").text(original);
}
</script>

关于jquery - 如果使用 JQuery 选中复选框,如何添加值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48380113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com