jQuery on click function nothing happening

倾然丶 夕夏残阳落幕 提交于 2019-12-24 09:37:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!