Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AJAX调用中发送DIV元素的ID_Javascript_Jquery_Ajax_Notifications - Fatal编程技术网

Javascript 在AJAX调用中发送DIV元素的ID

Javascript 在AJAX调用中发送DIV元素的ID,javascript,jquery,ajax,notifications,Javascript,Jquery,Ajax,Notifications,我正在为我的Laravel应用程序建立一个相对基本的通知系统,并且已经完成了生成和向某人显示通知的工作 现在,我正在通过构建功能来清除通知计数,并使用一些jQuery成功地做到了这一点。我需要进一步了解这一点,以获取正在显示的所有通知的ID,以便在AJAX调用中发送它们,以便将它们标记为在数据库中看到的 作为一个非常基本的复制和粘贴,我已经创建了一系列的功能。由于它缺少我的CSS文件和资源,它无法工作,或者看起来很像在我这边 在小提琴中,通知有一个id,以notification-id-X开头,

我正在为我的Laravel应用程序建立一个相对基本的通知系统,并且已经完成了生成和向某人显示通知的工作

现在,我正在通过构建功能来清除通知计数,并使用一些jQuery成功地做到了这一点。我需要进一步了解这一点,以获取正在显示的所有通知的ID,以便在AJAX调用中发送它们,以便将它们标记为在数据库中看到的

作为一个非常基本的复制和粘贴,我已经创建了一系列的功能。由于它缺少我的CSS文件和资源,它无法工作,或者看起来很像在我这边

在小提琴中,通知有一个
id
,以
notification-id-X
开头,其中
X
是显示的通知的id,而
数据id
属性显示了相同的id(我不确定是否需要)

从我的搜索结果来看,似乎我想使用下面的内容来搜索所有以通知id开头的
id

var notifications = $('[id^="notification-id-"]');

我只是不知道如何处理
notifications
对象,在我这样做之后,获取我找到的所有ID并将它们发送到AJAX调用中,我的Laravel应用程序可以读取它们。

你的
notifications
变成了一个jQuery对象,它包含与该选择器匹配的所有元素。只需通过这些元素循环并创建一个存储其ID的数组:

var notifications = $('[id^="notification-id-"]').map(function() {
    return this.id.slice(16);
}).get();
这将得到一个数组,其中包含变量
notifications
中的ID

要通过AJAX发送它们,请执行以下操作:

$.ajax({
    url: "my_server_script.php",
    method: "POST" // Or whatever you're using (GET, PUT, etc.)
    data: notifications // Let jQuery handle packing the data for you
    success: function(response) {
         // The data was sent successfully and the server has responded (may have failed server side)
    },
    error: function(xhr, textStatus, errorThrown) {
        // AJAX (sending data) failed
    },
    complete: function() {
        // Runs at the end (after success or error) and always runs
    }
});

您的
通知变成了一个jQuery对象,它包含与该选择器匹配的所有元素。只需通过这些元素循环并创建一个存储其ID的数组:

var notifications = $('[id^="notification-id-"]').map(function() {
    return this.id.slice(16);
}).get();
这将得到一个数组,其中包含变量
notifications
中的ID

要通过AJAX发送它们,请执行以下操作:

$.ajax({
    url: "my_server_script.php",
    method: "POST" // Or whatever you're using (GET, PUT, etc.)
    data: notifications // Let jQuery handle packing the data for you
    success: function(response) {
         // The data was sent successfully and the server has responded (may have failed server side)
    },
    error: function(xhr, textStatus, errorThrown) {
        // AJAX (sending data) failed
    },
    complete: function() {
        // Runs at the end (after success or error) and always runs
    }
});

谢谢工作起来很有魅力。我对JS不太在行,在这一天的大部分时间里我都在做这件事,很高兴终于解决了:)。@James没问题:)谢谢!工作起来很有魅力。我对JS不太在行,一天中大部分时间都在做这件事,很高兴终于解决了:)。@James没问题:)