繁体   English   中英

jQuery / JavaScript ajax调用以传递div的onClick变量

[英]jQuery/JavaScript ajax call to pass variables onClick of div

我试图将两个变量(如下)传递给php / MySQL“ update $ table SET ....”,而不刷新页面。

我希望div点击以传递以下变量

$read=0;
$user=$userNumber;

div基本上表示已读取一条消息,因此应更改颜色。

请问这样做的最好方法是什么?

这是一些使用jquery发布到页面并处理json响应的代码。 您必须创建一个PHP页面,该页面将接收发布请求并返回您想要执行的任何操作。

$(document).ready(function () {

    $.post("/yourpath/page.php", { read: "value1", user: $userNumber}, function (data) {
        if (data.success) {
          //do something with the returned json
        } else { 
          //do something if return is not successful
        } //if              
    }, "json"); //post
});

创建一个带有两个参数的php / jsp / .net页面

mywebsite.com/ajax.php?user=XXX&secondParam=ZZZZ

将onClick事件附加到DIV

$.get("ajax.php?user=XXX&secondParam=ZZZZ". function(data){
// here you can process your response and change DIV color if the request succeed
});

我不确定我是否理解。 参见$ .load() ;

使用更新代码制作一个新的php文件,然后仅返回一个json,说明它是否起作用。 您可以使用$ .getJSON jQuery函数来实现。

要基于jQuery中的ID从DOM中选择一个元素,只需执行以下操作:

$("#TheIdOfYourElement")

还是你的情况

$("#messageMenuUnread")

现在,要聆听单击它的时间,

$("#messageMenuUnread").click(function(){
   //DO SOMETHING
}

现在,享受AJAX的乐趣。 您可以阅读http://api.jquery.com/category/ajax/上的文档以获取更多技术详细信息,但这可以归结为

        $("#TheIdOfYourImage").click(function(){
            $.ajax({
              type: "POST",                                 // If you want to send information to the PHP file your calling, do you want it to be POST or GET. Just get rid of this if your not sending data to the file
              url: "some.php",                             // The location of the PHP file your calling
              data: "name=John&location=Boston",           // The information your passing in the variable1=value1&variable2=value2 pattern
              success: function(result){ alert(result) }   // When you get the information, what to do with it. In this case, an alert
            });
        }

至于颜色更改,您可以使用.css()方法更改CSS

$("#TheIdOfAnotherElement").css("background-color","red")

使用jQuery.ajax()

您的代码看起来像

<!DOCTYPE html>
<head>

</head>
<body>
    <!-- your button -->
    <div id="messageMenuUnread"></div>
    <!-- place to display result -->
    <div id="frame1" style="display:block;"></div>

    <!-- load jquery -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            //attach a function to messageMenuUnread div
            $('#messageMenuUnread').click (messageMenuUnread);
            //the messageMenuUnread function
            function messageMenuUnread() {
                $.ajax({
                    type: "POST",
                    //change the URL to what you need
                    url: "some.php",
                    data: { read: "0", user: "$userNumber" }
                }).done(function( msg ) {
                    //output the response to frame1
                    $("#frame1").html("Done!<br/>" + msg);
                });
            }
        }
    </script>
</body>

暂无
暂无

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

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