Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript } }); }); } }) }); });_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript } }); }); } }) }); });

Javascript } }); }); } }) }); });,javascript,jquery,ajax,Javascript,Jquery,Ajax,函数在JS中,因此 var fn = function() { ... } 在您的代码中,我看到唯一改变的是count的值 我会做下面的事情 var modifyItem = function(quantity) { return function() { event.preventDefault(); this_item = $(this).attr('title'); itemid = this_item.substr(0, this_item.length -

函数在JS中,因此

var fn = function() { ... }
在您的代码中,我看到唯一改变的是
count的值

我会做下面的事情

var modifyItem = function(quantity) {
  return function() {
    event.preventDefault();
    this_item = $(this).attr('title');
    itemid = this_item.substr(0, this_item.length - 1);
    count = parseInt($('#' + itemid).text());
    // note that quantity is added here
    count += quantity
    $.ajax({
      url: 'modifyproduct.php',
      type: 'GET',
      data: {
        location: location,
        itemid: itemid,
        count: count,
      },
      success: function(serverResponse) {
        $('#' + itemid).hide();
        $('#' + itemid).html(count).delay(170);
        $('#' + itemid).fadeIn();
      }
    });
  }
}
然后使用返回值(函数)作为要执行的操作

$('.glyphicon-plus').click(modifyItem(+1))
$('.glyphicon-minus').click(modifyItem(-1))
因此,函数是JS格式的

var fn = function() { ... }
在您的代码中,我看到唯一改变的是
count的值

我会做下面的事情

var modifyItem = function(quantity) {
  return function() {
    event.preventDefault();
    this_item = $(this).attr('title');
    itemid = this_item.substr(0, this_item.length - 1);
    count = parseInt($('#' + itemid).text());
    // note that quantity is added here
    count += quantity
    $.ajax({
      url: 'modifyproduct.php',
      type: 'GET',
      data: {
        location: location,
        itemid: itemid,
        count: count,
      },
      success: function(serverResponse) {
        $('#' + itemid).hide();
        $('#' + itemid).html(count).delay(170);
        $('#' + itemid).fadeIn();
      }
    });
  }
}
然后使用返回值(函数)作为要执行的操作

$('.glyphicon-plus').click(modifyItem(+1))
$('.glyphicon-minus').click(modifyItem(-1))

您不必在“.glyphicon plus”和“.glyphicon减号”上拥有事件处理程序的第二个副本;您只需要更改附加处理程序的方式

使用“on”而不是“click”,并绑定到始终存在的父元素,例如表、div,任何包装页面该部分的元素

“On”接受一个选择器:


这还有一个额外的好处,就是事件处理程序对于任何.glyphicon plus都是活动的,您可以随时通过DOM创建它。对于进行大量客户端更新的页面非常方便。

您不必在“.glyphicon plus”和“.glyphicon减号”上拥有事件处理程序的第二个副本;您只需要更改附加处理程序的方式

使用“on”而不是“click”,并绑定到始终存在的父元素,例如表、div,任何包装页面该部分的元素

“On”接受一个选择器:


这还有一个额外的好处,就是事件处理程序对于任何.glyphicon plus都是活动的,您可以随时通过DOM创建它。对于进行大量客户端更新的页面非常方便。

ajax调用的本质就是有点冗长冗长。只要每次都进行不同的ajax调用,这就是实现的方法。您可以将
get
调用
modifyproduct.php
分配给某个函数,然后调用该函数,这样就不必重复相同的代码。“在php中,我会创建一个类和函数来执行此任务,以避免代码重用。”只需对JS执行同样的操作,把逻辑分成几个函数,你可能会发现ajax调用的本质有点冗长。只要每次都进行不同的ajax调用,这就是实现的方法。您可以将
get
调用
modifyproduct.php
分配给某个函数,然后调用该函数,这样就不必重复相同的代码。“在php中,我会创建一个类和函数来执行此任务,以避免代码重用。”只需对JS执行同样的操作,将逻辑分成几个函数。你可能会在这方面有更好的运气