jquery - 是否可以使用 jQuery 选择器来访问 Struts2 元素而不需要插件?

标签 jquery jsp struts2

我是 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/

相关文章:

javascript - 使用 "rel"属性定位 AJAX 结果在正确的 DIV 中

java - 当获取 request.getParameter (""时,字符串无法转换为 ArrayList<String> )

java - 未显示 ActionError

Java Struts2 Action map 问题

Javascript slider ,如何为滚动分配最大值以使其不会溢出?

jquery - Highcharts 样式

java - Spring security 登录后如何跳转到另一个页面

struts2 - 如何为struts 2 Action 类编写junit测试?

java - 在struts 2对话框中显示错误消息?

jQuery 检查元素是否具有 css 属性