【问题标题】:how to use two $.post methods very well如何很好地使用两个 $.post 方法
【发布时间】:2014-01-09 16:16:49
【问题描述】:
 function ChangeGallery(){
    var GalleryName = $('.SubSubGalleryLock').text();

    /*Send string to Data1.php and include Tags from Database*/
    $.post("Data1.php", {  Sections: GalleryName  },
    function(data){
         $(".IncludeData").append(data);
    }); 

    /*send string to Data2.php and include Events data from Database*/
    $.post("Data2.php",{  GallerySec: GalleryName  },
    function(response){
         /*when i use alert method, this function works very well, why?*/
         alert('SomeString');
     var data = jQuery.parseJSON(response);
         var ImageID  = data[0];
         var ImageSrc = data[1];
     $(ImageID).click(function(){
        $(".LargeImage").attr('src', ImageSrc);
     });
     });
};

在Data1.php中

  /*give data from database1 and print to HTML File*/
  if ($_POST['Sections']) == "String")
  {  $results = mysql_query("SELECT * FROM Table1");
  while($row = mysql_fetch_array($results))
  { echo $row['Tags']; }

在Data2.php中

  /*give data from database2 and Use for events*/
  if ($_POST['GallerySec']) == "String")
  {  $results = mysql_query("SELECT * FROM Table2");
  while($row = mysql_fetch_array($results))
  { echo json_encode($row); }

当我在客户端使用它时,Data1.php 运行良好,但 Data2.php 仅在我编写警报时('Some stringh');在 var data = jQuery.parseJSON(response);线,它工作得很好,为什么?这个问题是什么原因造成的?你可以在这个页面看到它http://www.3dcreate.ir/Pages/Gallery/GalleryShow.php

【问题讨论】:

    标签: jquery html mysql ajax


    【解决方案1】:

    jQuery 不会在被调用后绑定到添加到 DOM 的新元素,除非您在添加新元素后重新绑定或者您最初绑定到父元素:

    $("#Chosen_Div-parent").on("mouseover", ".Chosen_Div", function(event){
      //Some codes
    });
    

    (如果您要拥有多个 Chosen_Div 元素,请使用类而不是 id)

    阅读docs

    【讨论】:

      【解决方案2】:

      Chosen_DIV 是一个 ID,而不是类。所以

      $('#Chosen_DIV').mouseover(function() {
          //SomeCodes
      })
      $('#Chosen_DIV').mouseout(function() {
          //SomeCodes
      })
      

      它必须是“#”而不是“。”

      【讨论】:

        【解决方案3】:

        你试图通过类名来访问元素,你应该通过 id 来访问:

        $('#Chosen_DIV').mouseover(function() {
            //SomeCodes
        })
        $('#Chosen_DIV').mouseout(function() {
            //SomeCodes
        })
        

        这应该可以工作

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-29
          • 1970-01-01
          • 2011-12-23
          • 2014-04-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多