【问题标题】:Dynamic input is not recognized in struts2-jquery tagsstruts2-jquery 标签中无法识别动态输入
【发布时间】:2013-08-16 09:42:35
【问题描述】:

在我的应用程序中,我需要动态更改<s:textfield> 的内容 我的代码如下:

<script type="text/javascript">
    $.subscribe('before', function(event, data) {                
        $('#text').val('Text Changed by jQuery');
    });               
</script>       

<s:form id="form2" action="textchange" >        
    <s:textfield id="text" name="text" value="Hello World!!!"/>                                                        
    <sj:submit  targets="result" onBeforeTopics="before" />
</s:form>                
   

我的预期输出是

Text Changed by jQuery

但我得到了

Hello World!!!

我正在使用 Struts2-jQuery-plugin 3.5.1。 如何获取动态输出?

【问题讨论】:

  • 你在哪里获得旧值?在您的操作类中还是在 jsp 本身中?
  • subscribe 放入文档就绪块中。
  • 我在 actionclass 中获得了我的旧值
  • 你需要ajax调用还是纯提交就够了?
  • 我需要使用 sj:submit 进行 ajax 调用。

标签: java jquery jsp struts2 struts2-jquery-plugin


【解决方案1】:

注意:这不是最好的方法。

但删除 subscribe 和 onBeforeTopics 属性。为提交按钮添加 id 并将点击事件绑定到它。

<script type="text/javascript">
  $(function(){
    $("#form2Submit").click(function() {   
      $('#text').val('Text Changed by jQuery');
    });
  });
</script>

<s:form id="form2" action="textchange">        
  <s:textfield id="text" name="text" value="Hello World!!!" />                                                        
  <sj:submit id="form2Submit" targets="result" />
</s:form>

任何其他方式。使用&lt;sj:a&gt; 标记与订阅。

<script type="text/javascript">
  $(function(){
    $.subscribe('before', function(event, data) {        
      $('#text').val('Text Changed by jQuery');
    }); 
  });
</script>

<s:form id="form2" action="textchange">        
  <s:textfield id="text" name="text" value="Hello World!!!" />                                                        
  <sj:a targets="result" onClickTopics="before" formIds="form2" button="true">
    Submit
  </sj:a>
</s:form>

【讨论】:

  • @Hariprasath:如果它有效,那有什么问题?接受/赞成这个答案。
  • @AleksandrM:我坚信如果有人反对投票,SO 应该强制发表评论
  • @UmeshAwasthi:那和有些人根本不应该投反对票。 :(
【解决方案2】:

before 在表单序列化后运行,所以使用旧值。你需要像这样修改代码

<script type="text/javascript">
  $(document).ready(function(){
   $('#before').click(function() {
       $('#text').val('Text Changed by jQuery');
   });
  });
</script>
<s:div id="result"/>
<s:form id="form2" action="textchange" method="POST">
<s:textfield id="text" name="text" value="Hello World!!!"/>
<sj:submit  id="before" targets="result" />
</s:form>

【讨论】:

  • 我尝试了很多方法,我不知道什么是正确的方法。请帮我在我的 ActionClass 中获取动态值。
  • @RomanC:我坚信如果有人反对投票,SO 应该强制发表评论
猜你喜欢
  • 2017-07-10
  • 2022-01-05
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2013-05-08
  • 2017-11-04
相关资源
最近更新 更多