如何在ASP.NET MVC中使用复选框单击来触发Ajax事件? [英] How to trigger an ajax event with a checkbox click in ASP.NET MVC?

查看:148
本文介绍了如何在ASP.NET MVC中使用复选框单击来触发Ajax事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了数据库,发现有人在谈论它,但没有证明事实 使用MVC中的HTML帮助器动态添加onclick

I searched the database, and I found someone talking about it but not demonstrating the fact Adding a onclick dynamically using an Html Helper in MVC

我想在复选框onclick事件上触发ajax

I would like to trigger ajax on a check box onclick event

 @Html.CheckBoxFor(model => model.HasChildren, new { onclick = "return Triger2();" })

我可以执行AJAX触发器,还是可以将我定向到将捕获click事件的jQuery方法.那么它将如何执行AJAX触发器?

Can I execute an AJAX trigger, or can someone please direct me to the jQuery method where I would catch the click event. Then how would it execute the AJAX trigger?

          <div id="Stream1"> @Ajax.ActionLink("Click", "Triger2", new AjaxOptions
            {
                UpdateTargetId = "Stream1",
                InsertionMode = InsertionMode.Replace,
                HttpMethod = "Get",
                LoadingElementId = "progress"
            })
         </div>

推荐答案

那么你可以做到

@Html.CheckBoxFor(model => model.HasChildren)

随后有下面的内容捕获复选框的单击并调用ajax.

later on have below to capture click of checkbox and call ajax.

$("#HasChildren").click(function(){
 $.ajax({

            url: '@Url.Action("actionmethod", "controllername")',
            type: "POST",
            data: //data,
            dataType: //type of response,
            success: function (data) {
//ur code here
}
error: function(data){

}
});});

}

注意::如果您想在未单击时调用ajax,但仅在选中或未选中值的情况下,才必须在click事件中使用 if 条件来检查相同使用$(this).is(:checked)

NOTE : if you wanna call ajax not on click but only if value is checked or not then you must use an if condition in the click event to check the same using $(this).is(:checked)

这篇关于如何在ASP.NET MVC中使用复选框单击来触发Ajax事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆