如何通过 jQuery 访问请求中发送的变量?

发布于 2024-10-03 03:55:26 字数 291 浏览 1 评论 0原文

我正在使用支柱。我的 ActionForm 中设置了一个 ArrayList,如何通过单击按钮时 jQuery 中的控制器将 ActionForm 发送到的 JSP 访问该数组。这样我就可以循环遍历该数组的元素。我猜是这样的,但那是在黑暗中刺伤(这是行不通的)。

$('myButton').click(function(){
    var myArrayToLoopThrough = $('myForm.myArray');
    for(){
        //looping stuff
    }
}

I'm using struts. My ActionForm has an ArrayList set up within it, how can I access the array from the JSP that the ActionForm is sent to by the controller in jQuery on a button click. This is so I can loop through the elements of that array. I guess its something like this, but that is a stab in the dark (which isn't working).

$('myButton').click(function(){
    var myArrayToLoopThrough = $('myForm.myArray');
    for(){
        //looping stuff
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

说不完的你爱 2024-10-10 03:55:26

jQuery 对 JSP 生成的 HTML 进行操作。

因此,请使用 Firebug for Firefox 等工具在浏览器中查看生成的 HTML。

然后您可以使用 jQuery 选择并迭代 HTML 元素。以下是最有用的内容的基本语法:

选择 ID$("#id")

按类别选择$(".class")

通过 HTML 标记选择$("p")$("span")

迭代选择

$(...something...).each(function(){
   // this is the DOM element
   // $(this) is a jQuery object containing the DOM element
});

有关选择器的官方 jQuery 文档


编辑

根据您的评论,您似乎正在寻找一种与服务器对象而不是生成的 HTML 进行通信的方法。

Javascript(jQuery 是用 Javascript 编写的)是一种 Web 浏览器语言,只能与生成的 HTML 进行交互。您的 Java 对象不会发送到浏览器。

如果您确实需要从服务器检索数据,那么您需要一个新的 HTTP 请求才能检索这些数据。这可以在 jQuery 中使用 AJAX 方法 来完成。

jQuery operates on the HTML generated by your JSP.

So have a look to the generated HTML in the browser using a tool like Firebug for Firefox.

Then you can use jQuery to select and iterate over HTML elements. Here are the basic syntax of the most useful stuff:

Select an ID: $("#id")

Select by class: $(".class")

Select by HTML tag: $("p") or $("span")

Iterate over a selection

$(...something...).each(function(){
   // this is the DOM element
   // $(this) is a jQuery object containing the DOM element
});

Official jQuery documentation on selectors


EDIT

Based on your comments, you seem to be looking for a way to communicate with server objects instead of generated HTML.

Javascript (jQuery is written in Javascript) is a web browser language that can only interact with generated HTML. Your Java objects are not sent to the browser.

If you do need to retrieve data from the server, then you need a new HTTP request in order to retrieve those data. This can be done in jQuery using the AJAX methods.

冰之心 2024-10-10 03:55:26

您可能需要查看 .serializeArray()。您可以将表单中的所有数据放入一个漂亮的对象中,这样您就可以对数据执行您想要的操作。

jQuery .serializeArray() 文档

var data = $('#form-id').serializeArray();

现在您可以循环访问数据。键是名称

You may want to check out .serializeArray(). You can get all the data from the form into a nice object so you can do what you want with the data.

jQuery .serializeArray() Documentation

var data = $('#form-id').serializeArray();

Now you can loop through data. The keys are name and value.

江挽川 2024-10-10 03:55:26

这是您要找的吗?

$('.myButton').click(function(e) {
    var data = $(this).closest('form').serializeArray();
    for( var i = 0; i < data.length; i++ ) {
        var field = data[i];
        console.log( field.name + '=>' + field.value );
    }
});

Is this what you're looking for?

$('.myButton').click(function(e) {
    var data = $(this).closest('form').serializeArray();
    for( var i = 0; i < data.length; i++ ) {
        var field = data[i];
        console.log( field.name + '=>' + field.value );
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文