在我的应用程序中,我需要动态更改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.
如何获得动态输出。
答案 0 :(得分:3)
注意:这不是最好的方法。
但是删除了subscribe和onBeforeTopics
属性。添加id以提交按钮并将click事件绑定到它。
<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>
答案 1 :(得分: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>