2013-08-16 18 views
2

在我的应用程序的认可,我需要改变动态 我的代码下面给出的textfield的内容:动态输入未在Struts2,jquery的标签

<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. 如何获取动态输出。

+0

你在哪里得到旧值?在你的行动类或jsp本身? –

+0

将'subscribe'放入文档就绪区块。 –

+1

即时获取我的行为类中的旧值 – Hariprasath

回答

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> 
+0

它可以工作,所以SO应该强制性地发表评论,还有没有其他办法可以实现这一点。 – Hariprasath

+0

@Hariprasath:如果它起作用,那么问题是什么?接受/ upvote这个答案。 –

+0

@AleksandrM:我坚信,如果有人投票,SO应该强制性地发表评论 –

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> 
+0

我已经尝试了很多方法,我不知道什么是正确的做法。请帮助我在ActionClass中获取动态值。 – Hariprasath

+0

@downvoter解释这个答案有什么问题? –

+0

@RomanC:我坚信,如果有人投票 –