简体   繁体   English

jQuery单击事件复选框更改了Ajax调用

[英]jQuery Click Event CheckBox Changed Ajax Call

I have three checkboxes that who have checked/unchecked values populated from a model. 我有三个复选框,它们均已从模型填充了已选中/未选中的值。 I'm using an Ajax post on button click event to call controller actions for each checkbox changed event in order to update the DB. 我正在使用Ajax按钮单击事件,以针对每个复选框已更改的事件调用控制器操作,以便更新数据库。

Here is the code for one of the checkboxes (apart from the selector ID, they are all the same): 以下是其中一个复选框的代码(除了选择器ID之外,它们都是相同的):

 $(document).ready(function () {

    //document.getElementById('UpdateButton').onclick = function () {
    $("UpdateButton").click = function () {
        $('#NatAm').change(function () {
            // if ($('#NatAm').is(':checked')) {
            $.ajax({
                //url: '@Url.Action("NativeUpdate", "Transactions")',
                    url: '/Transactions/NativeUpdate',
                    //data: { isNativeUp: true },

                    type: 'POST',
                    dataType: "json"
                });
                //}
            });

Edit (HTML/View Code): 编辑(HTML /查看代码):

 @Html.CheckBox("NatAm", (bool)@ViewBag.NativeAm)
 <input name="UpdateButton" id="UpdateButton" type="submit" value="Update" style="margin-left: 15px; margin-top: 3px;" class="btn btn-success" />

I cannot get this to work. 我无法使它正常工作。 Before adding the button, the ajax post was working fine. 在添加按钮之前,ajax发布工作正常。 Thank you for your help! 谢谢您的帮助!

Your click handler isn't right. 您的点击处理程序不正确。 You need to pass in the id of the button and use jQuery click handler. 您需要传递按钮的ID并使用jQuery单击处理程序。 You also need not to nest the handlers: 您也不需要嵌套处理程序:

$(document).ready(function() {
  $("#UpdateButton").click(update);
  $('#NatAm').change(update);
});

function update() {
  $.ajax({
    url: '/Transactions/NativeUpdate',
    type: 'POST',
    dataType: 'json'
  });
}

JSFiddle Demo: https://jsfiddle.net/vLzwuwdo/ JSFiddle演示: https ://jsfiddle.net/vLzwuwdo/

You're telling JQuery to look for 'UpdateButton' tags which in your case does not exist. 您要告诉JQuery查找'UpdateButton'标记,在您的情况下该标记不存在。 You're missing the # which indicates an ID in your button. 您缺少表示按钮ID的#号。

Try this 尝试这个

$(document).ready(function () {

//document.getElementById('UpdateButton').onclick = function () {
$("#UpdateButton").click(function () {
    $('#NatAm').change(function () {
        // if ($('#NatAm').is(':checked')) {
        $.ajax({
            //url: '@Url.Action("NativeUpdate", "Transactions")',
                url: '/Transactions/NativeUpdate',
                //data: { isNativeUp: true },

                type: 'POST',
                dataType: "json"
            });
            //}
        }));
  1. id should be unique in same document ( NatAm and UpdateButton ), replace the duplicate ones by global classes will solve the first problem. id在同一文档( NatAmUpdateButton )中应该是唯一的,用全局类替换重复的类将解决第一个问题。

  2. You should not define event inside another since every time you trigger the first it will create new event for the element, in your case every time you click new change event will be created and attached to the first element with NatAm . 您不应该在另一个事件中定义事件,因为每次触发第一个事件时,事件都会为该元素创建新事件,对于您而言,每次单击时都会创建新的更改事件,并使用NatAm附加到第一个元素。

Hope this helps. 希望这可以帮助。

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

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