表单或 select 的 onload() 函数是什么?

发布于 2024-12-18 04:24:02 字数 330 浏览 0 评论 0原文

我试图在 JavaScript 中找到一个每次创建

我想要这样的东西:

<body onload="jsFunction();">

更准确地说,我需要做一个

我在服务器端使用JSP

I'm trying to find a function in JavaScript that is called everytime when a <form> or <select> is created. Does anyone have a idea?

I want something like:

<body onload="jsFunction();">

More exactly, I need to make a <select> that will be included in many pages, and I want everytime this is loaded, to select one of it's values based on a request variable. So the function that will make the selection must called from this included script and must be included in this script.

I'm using JSP on the server side

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-12-25 04:24:02

这毫无意义。只需根据请求参数以编程方式设置整个 selected 属性即可。这是一个示例:

<select name="foo">
    <option value="bar" ${param.foo == 'bar' ? 'selected' : ''}>Bar</option>
    <option value="baz" ${param.foo == 'baz' ? 'selected' : ''}>Baz</option>
    <option value="waa" ${param.foo == 'waa' ? 'selected' : ''}>Waa</option>
</select>

或者如果您将其放在集合中:

<select name="foo">
  <c:forEach items="${foos}" var="foo">
    <option value="${foo}" ${param.foo == foo ? 'selected' : ''}>${foo}</option>
  </c:forEach>
</select>

对于普通输入也是如此:

<input type="text" name="bar" value="${fn:escapeXml(param.bar)}" />

fn:escapeXml() 是为了防止 XSS 攻击)

This makes no sense. Just set the whole selected attribute programmatically based on the request parameter. Here's an example:

<select name="foo">
    <option value="bar" ${param.foo == 'bar' ? 'selected' : ''}>Bar</option>
    <option value="baz" ${param.foo == 'baz' ? 'selected' : ''}>Baz</option>
    <option value="waa" ${param.foo == 'waa' ? 'selected' : ''}>Waa</option>
</select>

or if you have it in a collection:

<select name="foo">
  <c:forEach items="${foos}" var="foo">
    <option value="${foo}" ${param.foo == foo ? 'selected' : ''}>${foo}</option>
  </c:forEach>
</select>

The same is true for normal inputs:

<input type="text" name="bar" value="${fn:escapeXml(param.bar)}" />

(the fn:escapeXml() is there to prevent XSS attacks)

隔岸观火 2024-12-25 04:24:02

您可以尝试在默认情况下要选择的选项中使用“selected”属性(如此处的建议)。如果您更喜欢 javascript,则需要使用 jquery 之类的工具来选择(或填充并选择)您的标签。

供参考:

<select>
  <option>Volvo</option>
  <option selected="selected">Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>

You could try the "selected" attribute in the option you want to select by default (like suggested here). If you prefer javascript, you'll need to use something like jquery to select select (or populate and select) your tag.

For reference:

<select>
  <option>Volvo</option>
  <option selected="selected">Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文