问题
This jQuery function is meant to post the value of num to a PHP page and then echo it in the correct status class, any ideas why nothing is happening, I have downloaded jQuery and called the file further up (not displayed here)
Any help is appreciated thanks!
<script>
var num = 1;
function ajax_post(){
$.ajax('javas.php', {
success: function(response) {
$(".status").html(response);
},
data: "num=" + (++num)
});
}
function ajax_posta(){
$.ajax('javas.php', {
success: function(response) {
$(".status").html(response);
},
data: "num=" + (--num)
});
}
$(document).ready(function() {
$('.eventer > .button').click(function () {
ajax_post();
});
alert("lol");
});
</script>
This is what i have, this is my php code concerning the classes.
<div id = 'eventcontainer' >
<?php
//Getting posts from DB
$event1 = mysql_query("SELECT post,date,memid FROM postaction WHERE memid = '$id' ORDER BY date DESC LIMIT 5;");
while ($row1 = mysql_fetch_array($event1))
{
$event = $row1['post'];
$timeposted = $row1['date'];
$eventmemdata = mysql_query("SELECT id,firstname FROM users WHERE id = '$id' LIMIT 1");
while($rowaa = mysql_fetch_array($eventmemdata))
{
$name = $rowaa['firstname'];
$eventlist = "$event <br> $name";
}
echo " <div class = 'eventer'> $timeposted <br>$eventlist <input name='myBtn' type='submit' value='increment' onClick='javascript:ajax_post();'>
<input name='lol' type='submit' value='dec' onClick='javascript:ajax_posta();'></div>
<div class = 'status'></div>";
echo "<br>";
}
?>
回答1:
Instead of $.post change it to $.ajax
You could re-arrange the parameters to get it to work with $.post, but it looks like the code you have is meant to be run with $.ajax.
Also, inside of here
$(document).ready(function() {
$('.eventer > .button').click(function () {
var self = this;
$.post('javas.php', function (data) {
$(self).closest('.eventer').find('.status').html(data);
})
});
alert("lol");
});
Are you sure you didn't mean:
$(document).ready(function() {
$('.eventer > .button').click(function () {
ajax_post();
});
alert("lol");
});
回答2:
If you try to put an alert in the click action, what does it happen ?
Perhaps your action isn't added on the object you think.
回答3:
I think you have the order of the parameters in the call to $.post wrong. It should be:
$.post("url", { data:'something' }, function(result){
//callback
});
回答4:
It looks like you are mixing up the jQuery post and ajax methods. Try adding the data argument (your num variable) in your post.
$('.eventer > .button').click(function () {
var self = this;
$.post('javas.php', num,function (data) {
$(self).closest('.eventer').find('.status').html(data);
})
});
http://api.jquery.com/jQuery.post/
来源:https://stackoverflow.com/questions/8540305/jquery-on-click-function-nothing-happening