jQuery $ .post在添加函数(响应)时不起作用

时间:2011-04-19 10:36:33

标签: javascript jquery

我有以下js函数

function remove() {
    jQuery(document).ready(function() {
        jQuery("button").click(function(e) {
            var buttonHnd = jQuery(this);
            jQuery.post("script.php", {
            nume_client: jQuery("#nume_client").val()
            adresa: jQuery("#adresa").val(),
            comanda: jQuery("#comanda").val(),
            locatie: jQuery("#locatie").val(),
            istoric: jQuery("#istoric").val(),
            observatii: jQuery("#observatii").val()
        }, function(response) {
            alert("ok")
        },
        function(data) {
            buttonHnd.closest('tr').remove();
        });
    });
});
}

当我删除功能(响应)时,脚本会工作,它会删除该行。但我也想将它添加到数据库中。在script.php文件中,我只有带响应头的mysql查询。

第二个问题是我的删除按钮仅在第二次点击时有效,但仅在删除所有行后才有效。

任何人都可以帮助解决数据库问题吗?

您可以在此处找到script.php文件

修改

这篇文章中缺少一条线。该文件包含正确的代码。

2 个答案:

答案 0 :(得分:1)

<强>更新

截至您最近的编辑中添加了缺失的代码行,以下原始答案不再适用。仍然存在语法错误(在nume_client: jQuery("#nume_client").val()之后缺少逗号),但我认为这是一个复制粘贴错误。添加逗号使其在语法上有效。

您的代码正在做的是,每次调用remove时,它都要求jQuery在文档“准备就绪”时运行一个函数。只要你在文档准备就绪之前永远不会调用remove (或者只执行一次),那大多数情况下都会很好,因为之后jQuery会立即调用该函数。

该功能的作用是在页面上的每个button元素上设置一个点击处理程序。然后(如果再次调用它)它再次设置处理程序 ,所以现在页面上的每个button都有两个处理程序,每个处理程序都会尝试执行相同的操作单击按钮时。如果你第三次调用它,你将在页面上的每个button上都有三个处理程序,所有人都试图在点击任何按钮时做同样的事情。

一旦您至少调用remove一次,页面上的所有button都会通过尝试执行post来响应点击,并且(成功时)删除最近的tr包含jQuery(document).ready(function() { jQuery("button").click(function(e) { var buttonHnd = jQuery(this); jQuery.post("script.php", { nume_client: jQuery("#nume_client").val(), adresa: jQuery("#adresa").val(), comanda: jQuery("#comanda").val(), locatie: jQuery("#locatie").val(), istoric: jQuery("#istoric").val(), observatii: jQuery("#observatii").val() }, function(data) { buttonHnd.closest('tr').remove(); } ); }); });

以下是我认为您的意思:

remove

的变化:

  1. 完全删除函数script,因此只要在页面上遇到此代码,就会立即执行此代码,并设置在DOM准备就绪时要调用的函数。确保在包含jQuery的remove标记后面有代码。并且您将要修改调用button的代码以便不调用它。
  2. 删除第二个功能,留下第三个功能。 signature for jQuery.post接受URL,要发布的数据,函数以及可选的数据类型字符串。你传递的是最后一个参数的函数,这个函数不是(据我所知)支持的,几乎肯定不是你想要做的。 : - )
  3. 这仍然会将其连接到页面上的每个按钮,这可能是您不想要的。相反,您可能希望使选择器更具体(例如,使用ID,使用结构选择器,使用类等;使用jQuery,您有lots of choices如何缩小它)。

    虽然这样可行,但它还会在tr元素上留下悬空事件处理程序,以便在删除delegate时将其删除。对于处理动态表,您可能希望使用delegatelivelive将事件挂钩到容器元素上,但只有在事件由与您提供的选择器匹配的元素生成时才触发代码。只有一个事件处理程序,但它的行为很像在与选择器匹配的元素上有一个处理程序。 delegate基本上是delegate,使用文档根作为容器。

    在您的情况下,我可能会在table元素上使用button[name="remove"],并使用<table id='theTable'> <tbody> <tr> <td>Blah blah blah</td> <td><button name='remove'>Remove</button></td> </tr> <tr> <td>Blah blah blah</td> <td><button name='remove'>Remove</button></td> </tr> </tbody> </table> 行的选择器。示例HTML:

    jQuery(document).ready(function() {
        jQuery("#theTable").delegate('button[name="remove"]', 'click', function(e) {
            var buttonHnd = jQuery(this);
            jQuery.post("script.php",
                {
                    nume_client: jQuery("#nume_client").val(),
                    adresa:      jQuery("#adresa").val(),
                    comanda:     jQuery("#comanda").val(),
                    locatie:     jQuery("#locatie").val(),
                    istoric:     jQuery("#istoric").val(),
                    observatii:  jQuery("#observatii").val()
                },
                function(data) {
                    buttonHnd.closest('tr').remove();
                }
            );
        });
    });
    

    使用该示例HTML,然后:

    name

    您不必使用button,只要您能够确定哪些button元素应以这种方式响应点击。如果表格中所有jQuery('#theTable').delegate('button', 'click', function...元素都是移除按钮,请使用$sql="INSERT INTO `baza`.`tabel` (`1`, `2`, `3`) VALUES ('".$arr['adresa']."', '".$arr['nume_client']."', '".$arr['comanda']."')";

    除了更新

    您让我看一下script.php文件。对不起,我不是一个PHP主管,但FWIW,剧本的主要内容是:

    $arr['adresa']

    观察:

    1. 你没有做任何事情来逃避$arr['adresa']等中的值。有两个大问题没有做到:

      1. 如果(例如)'或任何其他字段中包含baza.tabel,查询将会中断。

      2. 你对SQL injection attacks敞开大门。阅读php.net page on SQL injection以及如何避免它。

    2. 1 中的列确实的名称是23nume_client吗?这是一个非常奇怪的表结构。通常情况下,我希望看到包含(例如)adresacomandafunction remove() { jQuery(document).ready(function() { jQuery("button").click(function(e) { var buttonHnd = jQuery(this); nume_client: jQuery("#nume_client").val() // ^-- Here you're creating a label, I suspect you were trying to create a property adresa: jQuery("#adresa").val(), // ^-- And here comanda: jQuery("#comanda").val(), // ^-- And here, I expect this one's actually a syntax error, // check your browser's JavaScript console for errors locatie: jQuery("#locatie").val(), // ^-- And here istoric: jQuery("#istoric").val(), // ^-- And here observatii: jQuery("#observatii").val() // ^-- And here }, function(response) { // <== Here you're passing a second function into `click` alert("ok") }, function(data) { // <=== Here you're passing a *third* function into `click` buttonHnd.closest('tr').remove(); }); }); }); } 等名称的列。


    3. 原始回答

      您在该代码中遇到了许多问题:

      function remove() {
      
          jQuery(document).ready(function() {
      
              jQuery("button").click(function(e) {
      
                  var buttonHnd = jQuery(this);
      
                  jQuery.post(some_url_here,
                      {
                          nume_client: jQuery("#nume_client").val(),
                          adresa: jQuery("#adresa").val(),
                          comanda: jQuery("#comanda").val(),
                          locatie: jQuery("#locatie").val(),
                          istoric: jQuery("#istoric").val(),
                          observatii: jQuery("#observatii").val()
                      },
                      function(response) {
                          buttonHnd.closest('tr').remove();
                      }
                  );
              });
          });
      }
      

      guess ,我会说你可能想要这样的东西:

      remove

      ...但你不希望它全部包含在{{1}}函数中,我很确定。

答案 1 :(得分:0)

这会破坏所有脚本(可能),因为格式不正确

            nume_client: jQuery("#nume_client").val()
            adresa: jQuery("#adresa").val(),
            comanda: jQuery("#comanda").val(),
            locatie: jQuery("#locatie").val(),
            istoric: jQuery("#istoric").val(),
            observatii: jQuery("#observatii").val()

它无用,就像你应该这样做,或者至少就是我认为你想要做的事情。

var obj = {nume_client: jQuery("#nume_client").val()
            adresa: jQuery("#adresa").val(),
            comanda: jQuery("#comanda").val(),
            locatie: jQuery("#locatie").val(),
            istoric: jQuery("#istoric").val(),
            observatii: jQuery("#observatii").val()
};

修改 无论如何我认为你要做的是某种类型的ajax

 jQuery("button").click(function(e) {
        var buttonHnd = jQuery(this);
        var data =  {
          nume_client: jQuery("#nume_client").val()
          adresa: jQuery("#adresa").val(),
          comanda: jQuery("#comanda").val(),
          locatie: jQuery("#locatie").val(),
          istoric: jQuery("#istoric").val(),
          observatii: jQuery("#observatii").val()
       };
       $.ajax({
             url: 'script.php',
             type: 'post',
             data:data,
             success: function (response){
                   alert('ok')
             },
             error:function (){
                  //in case you get a 500,404 error etcc 
             }
       });
    },
    function(data) {
        buttonHnd.closest('tr').remove();
    });