I'm beginner in php jquery ajax and something confuses me. I want to send data with GET function over jquery/Ajax
This is the code:
<html>
<head>
<script type="text/javascript" src="jquery.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
var number = 0;
$('a.sumnumber').click(function(e) {
e.preventDefault();
number += 1;
//alert('dfsdgsdgsg ' + number);
$('.sum').load('sum_number.php?sum=' + number);
});
$('a.minusnumber').click(function(e) {
e.preventDefault();
$('.sum').load('sum_number.php?minus=' + number);
});
});
</script>
</head>
<body>
<a href="#" class="sumnumber">+</a>
<br />
<br />
<div class="sum">
<p>dfgdfgdfg</p>
</div>
</body>
&开发者_高级运维lt;/html>
sum_number.php
<?php
$sum = 0;
if(isset($_GET['sum']))
{
$sum += $_GET['sum'];
echo $sum;
}
echo '
<br />
<a href="#" class="minusnumber">-</a>
<br />
';
$minus = 100;
if(isset($_GET['minus']))
{
$minus -= $_GET['minus'];
echo $minus;
}
?>
When i click on link with class sumnumber it works but when i click on link with class minusnumber that was loaded by jquery from sum_number.php nothing happen. Is it possible to make it work?
Use jQuery's live
$('a.minusnumber').live('click', function(e) {
e.preventDefault();
$('.sum').load('sum_number.php?minus=' + number);
});
If you load in minusnumber with javascript. You need to use bind or live on the click event. Something like this. Dont forget to check the links too!
$('a.minusnumber').click(function(e) {
e.preventDefault();
$('.sum').load('sum_number.php?minus=' + number);
});
Sould be
$('a.minusnumber').live('click', function(e) {
e.preventDefault();
$('.sum').load('sum_number.php?minus=' + number);
});
or
$('a.minusnumber').bind('click', function(e) {
e.preventDefault();
$('.sum').load('sum_number.php?minus=' + number);
});
http://api.jquery.com/bind http://api.jquery.com/live/
精彩评论