简体   繁体   English

使用仅运行一次的jquery加载函数以及如何传递变量来重新加载div

[英]Reload div using jquery load function working only once and how to pass a variable

I have very limited knowledge with scripts so I hope you guys can help me with a simple solution to a small problem that I have... 我对脚本的知识非常有限,所以我希望你们能帮助我解决一个小问题的简单解决方案......

I'm using the following jquery function to refresh a div with new content when a link is clicked 我正在使用以下jquery函数在单击链接时使用新内容刷新div

<script> 
$(function() {       
$("#myButton").click(function() {
$("#loaddiv").fadeOut('slow').load("reload.php").fadeIn("slow");
});
});
</script>

My problem is, I need to send 2 variables to the reload.php page to use in a mysql query (I have no idea how to accomplish that), also I need to make multiple links work with this function, at the moment I have multiples links with the same id and only the first link works so I guess I must associate different ids to the function in order for this to work, how can I do that? 我的问题是,我需要将2个变量发送到reload.php页面以在mysql查询中使用(我不知道如何实现),我还需要让多个链接与此函数一起工作,此刻我有多个具有相同ID的链接,只有第一个链接有效,所以我想我必须将不同的id与函数关联才能使其工作,我该怎么做?

here's the page where i'm using this: http://www.emulegion.info/teste/games/game.php 这是我正在使用的页面: http//www.emulegion.info/teste/games/game.php

You may want to use document ready instead of function on your first line as this will make sure the code is not executed until the full page (and all elements) have loaded. 您可能希望在第一行使用文档就绪而不是函数,因为这将确保在整页(和所有元素)加载之前不执行代码。

You can then use the callback functions of the fade and load to perform actions in a timely manner. 然后,您可以使用淡入淡出和加载的回调函数来及时执行操作。

additional variables you can add after the .php, these can then be read in your reload.php file as $var1 = $_GET['var1']; 您可以在.php之后添加其他变量,然后可以在reload.php文件中将这些变量读取为$ var1 = $ _GET ['var1']; Do make sure to sanitize these though for security. 为了安全起见,请确保对这些进行消毒。

<script type="text/javascript">

// execute when document is ready
$(document).ready(function() {

  // add click handler to your button
  $("#myButton").click(function() {

    // fade div out
    $("#loaddiv").fadeOut('slow',function(){

      // load new content
      $("#loaddiv").load("reload.php?var1=foo&var2=bar",function(){

        // content has finished loading, fade div in.
        $("#loaddiv").fadeIn('slow');

      }); // end load content

    });  // end fade div out

  }); // end add click to button

}); // end document ready

</script>

For different variables you could add a HTML5 style variable to your button. 对于不同的变量,您可以在按钮中添加HTML5样式变量。

<input type="button" id="myButton" data-var1="foo" data-var2="bar" />

You can retrieve this when the button is clicked: 单击按钮时可以检索此项:

// add click handler to your button
  $("#myButton").click(function() {

  // get vars to use
  var var1 = $(this).data('var1');
  var var2 = $(this).data('var2');

  ... 

  load("reload.php?var1="+var1+"&var2="+var2

if you have multiple buttons/links I would use class instead of id "myButton". 如果你有多个按钮/链接我会使用类而不是id“myButton”。 that way you can apply the function to all buttons with the above script. 这样你可以使用上面的脚本将函数应用于所有按钮。 Just replace "#myButton" for ".myButton" 只需将“#myButton”替换为“.myButton”

First, you should use .on('click', function() or .live('click', function() to resolve your one click issue. 首先,您应该使用.on('click',function()或.live('click',function()来解决您的单击问题。

You'll want to do something like: 你会想要做的事情:

<script> 
$(function() {       
$("#myButton").on('click', function() {
var a = 'somthing';
var b = 'something_else';
$.post('url.php', {param1: a, param2: b}, function(data) {
   //data = url.php response
  if(data != '') {
   $("#loaddiv").fadeOut('slow').html(data).fadeIn("slow");
  }
});
});
});
</script>

Then you can just put var_dump($_POST); 然后你可以把var_dump($_POST); in url.php to find out what data is being sent. 在url.php中找出正在发送的数据。

Try creating a function that would accept parameters that you want. 尝试创建一个可以接受所需参数的function Like: 喜欢:

$(document).ready(function(){
    $('.link').click(function(){
        reload(p1,p2);
    });
});

function reload(param1, param2){
    $("#loaddiv").fadeOut('slow').load("reload.php?param1="+param1+"&param2="+param2).fadeIn("slow");
}

But by doing the above code your reload.php should be using $GET . 但是通过执行上面的代码,你的reload.php应该使用$GET Also you need to use class names for your links instead of id . 您还需要为链接使用class名而不是id

<script type="text/javascript">

// execute when document is ready
**$(document).ready(function() {**

  **$("#myButton").click(function() {**

    **$("#loaddiv").fadeOut('slow',function(){**

      **$("#loaddiv").load("reload.php?var1=foo&var2=bar",function(){**

        // content has finished loading, fade div in.
        $("#loaddiv").fadeIn('slow');

      }); 

    });  
  }); 
}); 

</script>
  $("#myButton").click(function() {

  // get vars to use
  var var1 = $(this).data('var1');
  var var2 = $(this).data('var2');

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

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