2

在我的应用程序中,我需要<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。如何获得动态输出?

4

2 回答 2

3

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

但删除订阅和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>

任何其他方式。<sj:a>标签与订阅一起使用。

<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>
于 2013-08-16T10:19:48.303 回答
1

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>
于 2013-08-16T09:53:03.673 回答