简体   繁体   English

我如何向此复选框添加点击事件? (HTML 助手)

[英]How could i add an on click event to this checkbox? (HTML Helpers)

I have a checkbox as follows:我有一个复选框,如下所示:

@using (Html.BeginForm("ItemsEdit", "Items", FormMethod.Post))
       {

            @Html.CheckBox("itemCheck")

       }

I want to be able to send an additional value to a controller when it is clicked, similar to something like this:我希望能够在单击时向 controller 发送附加值,类似于以下内容:

<input id = "@dataLineItem.Split(Model.ItemList.delimiterChar)[0]" type="checkbox" value="true" name="Remember" onclick="sendCheckId(this.id)" />

Below is the jQuery i am using to pass this id value to my controller:下面是我用来将此 id 值传递给我的 controller 的 jQuery:

function sendCheckId(checkedId) {
        var sentId = checkedId;
        $.post("/Items/ItemsEdit/", { sendIdToAction: sentId })
    }

However, if i were to use the method above, i wouldnt know how to pass the 'true/false' values of the checkbox, to the same controller.但是,如果我使用上述方法,我将不知道如何将复选框的“真/假”值传递给相同的 controller。

Maybe try onclick="sendCheckId(this.id,this.value)"也许试试onclick="sendCheckId(this.id,this.value)"

I'm not sure what that sendCheckId function leads to, but it should be pretty quick and easy to expand it to include the second value.我不确定 sendCheckId function 会导致什么,但是将其扩展为包含第二个值应该非常快速且容易。

I found out that i could simply do this using the Razor engine:我发现我可以使用 Razor 引擎简单地做到这一点:

@Html.CheckBox("itemCheck", new
{
     onclick = "alert('Hello')"
}

Credit: https://www.c-sharpcorner.com/forums/how-to-call-on-click-event-in-mvc-5信用: https://www.c-sharpcorner.com/forums/how-to-call-on-click-event-in-mvc-5

(The answer by Ramesh Palanivel) (拉梅什·帕拉尼维尔的回答)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM