我是 Struts2 的新手,但我一直遇到 jQuery 选择器无法访问我们在 jsps 中定义的任何 Struts2 标签的问题。
我假设如果没有大量自定义代码或插件,它们就无法集成,但我无法找到任何关于它的明确内容。当我去strut2-jQuery plugin他们提到最好在常见问题解答中使用插件,但不是必需的。
我在这里遗漏了什么吗?如果我有一个元素的 struts2 标签并尝试使用 jQuery 选择它,它似乎不起作用。我说的是基本元素,例如
$('form').find(':input').change(function() { ... //or
$('#testbutton').attr('disabled', true); //or
$('#formName').change(function() {
alert("Found form!");
});
因此,当我查看源代码时,我看到从标签生成的常规 html 代码,如果它是来自 struts2 标签与具有相同 jQuery 的常规 html 标签,我只是无法使用选择器访问这些特定元素选择器代码。
另一项,我使用类似的东西在 jsp 页面上引用 jQuery:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
最佳答案
HTML 的生成方式与客户端(浏览器)无关,并且客户端不知道生成的 DOM 之外的任何内容。
如果有一个 ID,或者它是一个输入,jQuery 会找到它——多年来我一直在 S2 应用程序中使用纯 jQuery(和 Prototype),没有出现任何问题。
还有其他问题。
已编辑以显示 JSP、呈现的 HTML 和控制台输出。
JSP:
<s:form action="simpleForm">
<s:textfield name="fname"/>
<s:submit value="Go!"/>
</s:form>
渲染的 HTML(假设默认 "xhtml"
主题):
<form id="simpleForm" name="simpleForm" action="/simpleForm.action" method="post">
<table class="wwFormTable">
<tr>
<td class="tdLabel"></td>
<td><input type="text" name="fname" value="" id="simpleForm_fname"/></td>
</tr>
<tr>
<td colspan="2"><div align="right"><input type="submit" id="simpleForm_0" value="Go!"/>
</div></td>
</tr>
</table></form>
一些控制台输出:
> $("form")
[<form id="simpleForm" name="simpleForm" action="/simpleForm.action" method="post">…</form>]
> $("form").find(":input")
[<input type="text" name="fname" value="d" id="simpleForm_fname">,
<input type="submit" id="simpleForm_0" value="Go!">]
> $("form").find("input") // Don't need the ":"
[<input type="text" name="fname" value="d" id="simpleForm_fname">,
<input type="submit" id="simpleForm_0" value="Go!">]
关于jquery - 是否可以使用 jQuery 选择器来访问 Struts2 元素而不需要插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10488912/