简体   繁体   English

如何更改textfield + jQuery + Struts2的值

[英]how to change the value of textfield + jQuery + Struts2

I have two textfield A , B : i want to do something like when i enter something in textfield A,this value will be use it in some action and result will be displayed in textfield B without clicking the submit button using ajax. 我有两个文本字段A,B:我想做一些事情,例如当我在文本字段A中输入内容时,此值将在某些操作中使用它,结果将显示在文本字段B中,而无需使用Ajax单击提交按钮。 how can i do it please ? 我该怎么办? note that i am a using struts2 请注意,我正在使用struts2

Most of the information has already been provided by @alexanderb and i believe that Jquery is good way to go, now lets come to the second half of your question about using AJAX in your code. @alexanderb已经提供了大多数信息,我相信Jquery是不错的选择,现在让我们来谈谈关于在代码中使用AJAX的问题的下半部分。 there are few ways you can send results from your action class. 您可以通过几种方法来发送操作类的结果。

  1. Return JSON from your action class and use above code. 从操作类返回JSON并使用上面的代码。
  2. Use stream result type in your S2 code and place the data in the textfield. 在S2代码中使用流结果类型,然后将数据放在文本字段中。

Still i believe JSON with Jquery is good way to go which not only provides you the feasibility to easily extend functionality in future but also provide a clean way.Struts2 provides a plugin which can convert the data being send from your action class to JSON and all you will be left to parse the JSON data in your UI to fill the text-box.For details how to work with JSON in s2 refer to JSON plugin for detail 我仍然相信使用Jquery进行JSON是一个好方法,它不仅为您提供了将来轻松扩展功能的可行性,而且还提供了一种简洁的方法.Struts2提供了一个插件,可以将从您的操作类发送的数据转换为JSON以及所有您将需要在用户界面中解析JSON数据以填充文本框。有关如何在s2中使用JSON的详细信息,请参阅JSON插件。

With JSON plugin your flow will be 使用JSON插件,您的流程将

  • Call your Action class on specific event in text box. 在文本框中针对特定事件调用Action类。
  • Configure your action to return JSON data using S2-JSon plugin. 使用S2-JSon插件配置您的操作以返回JSON数据。
  • Action will return JSON to the Jquery code. 动作会将JSON返回到Jquery代码。
  • Parse the JSON data and fill the text box with the value 解析JSON数据并在文本框中填充值

It should be easy. 应该很容易。 Suppose you have action that takes value as string and return some string back, availble on '/app/service' url. 假设您有将值作为字符串并返回一些字符串的操作,该字符串可在“ / app / service” URL上使用。

You can create such code for that: 您可以为此创建以下代码:

$(function() {

  $('#text_1').on('keyup', function() {

     var value = $(this).val();
     $.post('/app/service', JSON.stringify(value), function (r) {
       $('text_1').text(r);
     }); 

  });

});

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

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