单击按钮中的类中的Echo数据

时间:2011-12-16 18:39:44

标签: jquery html css ajax class

这是一个ajax函数,我想在点击特定类中的按钮时执行,许多相同的类是从php中的while循环生成的,但是我希望数据只在类中输出在按钮中单击按钮,而不是其他类,尽管它们具有相同的名称。

我还需要更改ajax函数,因为“status”是一个不是id的类,而函数调用ID“status”的元素而不是类“status”,

<script>
    function ajax_posta() {
// Create our XMLHttpRequest object
        var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
        var url = "javas.php";


        hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
        hr.onreadystatechange = function () {
            if (hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                document.getElementById('status').innerHTML = return_data;
            }
        }
// Send the data to PHP now... and wait for response to update the status div
        hr.send("num=" + (--num)); // Actually execute the request
        document.getElementById('status').innerHTML = "<img src = 'loading.gif' height = '30'     width = '30'>";

    }
    $(document).ready(function()
    {
        $('.eventer.button').click(function () {
            var self = this;
            $.post('javas.php', function (data) {
                $(self).parent('div').find('.status').html(data);
            })
        });
    }
    )
    ;
</script>

while循环中的PHP代码,生成具有相同名称的类和按钮的多个副本。

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 个答案:

答案 0 :(得分:0)

试试这个:

    $('.eventer > .button').click(function () {
        var self = this;
        $.post('javas.php', function (data) {
            $(self).closest('.eventer').find('.status').html(data);
        })
    });

将ajaxposta()事物转换为jQuery。 = d

也许是这样的:

function ajax_post() {  
    $.ajax({
        type: "POST",
        url: "javas.php",
        data: "num=" + (--num),
        success: function(result){
        $('#status').html(result);
    });
    $('#status').html("<img src=\"loading.gif\" style=\"height:30px; width:30px;\">");
}
相关问题