how to use two $.post methods very well

2020-01-19 05:18发布

 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);
     });
     });
};

in 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']; }

in 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); }

in Client side when i use it then Data1.php works very well but Data2.php only when i write an alert('Some stringh'); after var data = jQuery.parseJSON(response); line, it's work well, why? what's due to this problem?you can see it in this page http://www.3dcreate.ir/Pages/Gallery/GalleryShow.php

3条回答
Animai°情兽
2楼-- · 2020-01-19 05:37

You're trying to access to the element as by class name, you should do it by id:

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

this should work

查看更多
爷的心禁止访问
3楼-- · 2020-01-19 06:00

Chosen_DIV is a ID, not class. So

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

It has to be '#' not '.'

查看更多
我想做一个坏孩纸
4楼-- · 2020-01-19 06:00

jQuery won't bind to new elements added to the DOM after being called unless you rebind after they've been added or you bind to the parent element initially:

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

(If you're ever going to have more than one Chosen_Div element, use classes instead of ids)

Read the docs

查看更多
登录 后发表回答