jQuery .Ajax()函数选择器,将特定数据存储在变量中

时间:2020-12-19 15:42:42

I am new to jQuery and I have the following problem.

我是jQuery的新手,我有以下问题。

My project has say 2 pages, 1.JSP and 2.html. Now I want to pick selected data from 2.html and use it on 1.JSP. Now this was achieved very easily using .load but I want the data to be present in a JavaScript variable rather than put it on the page (div tags, etc.), so that I can work upon that data (modify or add to database).

我的项目有2页,1.JSP和2.html。现在我想从2.html中选择所选数据并在1.JSP上使用它。现在这很容易使用.load实现,但我希望数据存在于JavaScript变量中,而不是将其放在页面上(div标签等),以便我可以处理该数据(修改或添加到数据库) )。

I tried using .ajax and was able to write the following code:

我尝试使用.ajax并能够编写以下代码:

    var value = (function () {
        var val = nulll;
        var filename = " 2.html";

        $.ajax ({
            'async': false,
            'global': false,
            'url': filename,
            'success' : function(data) {
                val = data;
            }
        })
        return val;
    })()
    document.write(value)

Where do I put the selector format (say div.id5) so that my variable have only relevant data rather than the full file data?

我在哪里放置选择器格式(比如div.id5),以便我的变量只有相关数据而不是完整的文件数据?

3 个解决方案

#1


1  

function modify_data(data){
....
}

$.ajax({
   type: "POST", //POST OR GET
   url: "1.JSP", // URL TO SEND AJAX DATA TO
   data: "name=John&location=Boston", // DATA TO SEND
   success: function(data){ // CALLBACK FUNCTION
     modify_data(data);// sending data to another function to play with it
     $('body').html(data);
   }
 });

This is how to send an Ajax request and print it on the body after modifying the received data.

这是在修改接收的数据后发送Ajax请求并将其打印在正文上的方法。

#2


0  

Let's say you have 1.html and 2.html, and inside 2.html's body you have this:

假设您有1.html和2.html,并且在2.html的正文中你有这个:

<body>
<h1 id="foo">hello</h1>
</body>

Then in your 1.html you'd get the text inside H1 like this:

然后在你的1.html中,你会得到H1里面的文字,如下所示:

var filename = " 2.html";

$.ajax ({
    'async': false,
    'global': false,
    'url': filename,
    'success' : function(data) {
        var html = $(data);
        html.each(function() {
            if(this.tagName && this.tagName == 'H1') {
                alert( $(this).html() ); 
            }
        })
    }
})

This should give an annoying JS alert with 'hello' written inside it.

这应该给出一个讨厌的JS警报,里面写着'hello'。

#3


0  

I guess you're referring to $.load() function's ability to receive a jquery selector along with the URL to filter the result. That's to say, today you're doing it like this:

我猜你是指$ .load()函数接收jquery选择器以及过滤结果的URL的能力。那就是说,今天你这样做:

$('div').load('2.html div.id5');

So you really want to be able to do the same using the $.ajax() function. I believe the easiest way for you to do that is to use the .find() jquery function inside your 'success' event function (I'm omitting the whole .ajax() call, just typing the success event code):

所以你真的希望能够使用$ .ajax()函数做同样的事情。我相信你最简单的方法就是在'success'事件函数中使用.find()jquery函数(我省略了整个.ajax()调用,只输入成功事件代码):

success: function (data) {
    val = $(data).find('div.id5').html(); 
    // now you ony have that specific div's contents
}

I saw a few other errors in your javascript code I think you'll want to fix (if you're actually using that code).

我在你的javascript代码中看到了一些其他错误,我认为你会想要修复(如果你实际上正在使用该代码)。

    // null is defined with 2 ls
    var val = nulll;

    // Is the space on purpose?
    var filename = " 2.html";

#1


1  

function modify_data(data){
....
}

$.ajax({
   type: "POST", //POST OR GET
   url: "1.JSP", // URL TO SEND AJAX DATA TO
   data: "name=John&location=Boston", // DATA TO SEND
   success: function(data){ // CALLBACK FUNCTION
     modify_data(data);// sending data to another function to play with it
     $('body').html(data);
   }
 });

This is how to send an Ajax request and print it on the body after modifying the received data.

这是在修改接收的数据后发送Ajax请求并将其打印在正文上的方法。

#2


0  

Let's say you have 1.html and 2.html, and inside 2.html's body you have this:

假设您有1.html和2.html,并且在2.html的正文中你有这个:

<body>
<h1 id="foo">hello</h1>
</body>

Then in your 1.html you'd get the text inside H1 like this:

然后在你的1.html中,你会得到H1里面的文字,如下所示:

var filename = " 2.html";

$.ajax ({
    'async': false,
    'global': false,
    'url': filename,
    'success' : function(data) {
        var html = $(data);
        html.each(function() {
            if(this.tagName && this.tagName == 'H1') {
                alert( $(this).html() ); 
            }
        })
    }
})

This should give an annoying JS alert with 'hello' written inside it.

这应该给出一个讨厌的JS警报,里面写着'hello'。

#3


0  

I guess you're referring to $.load() function's ability to receive a jquery selector along with the URL to filter the result. That's to say, today you're doing it like this:

我猜你是指$ .load()函数接收jquery选择器以及过滤结果的URL的能力。那就是说,今天你这样做:

$('div').load('2.html div.id5');

So you really want to be able to do the same using the $.ajax() function. I believe the easiest way for you to do that is to use the .find() jquery function inside your 'success' event function (I'm omitting the whole .ajax() call, just typing the success event code):

所以你真的希望能够使用$ .ajax()函数做同样的事情。我相信你最简单的方法就是在'success'事件函数中使用.find()jquery函数(我省略了整个.ajax()调用,只输入成功事件代码):

success: function (data) {
    val = $(data).find('div.id5').html(); 
    // now you ony have that specific div's contents
}

I saw a few other errors in your javascript code I think you'll want to fix (if you're actually using that code).

我在你的javascript代码中看到了一些其他错误,我认为你会想要修复(如果你实际上正在使用该代码)。

    // null is defined with 2 ls
    var val = nulll;

    // Is the space on purpose?
    var filename = " 2.html";