重新加载页面而不将其提交回服务器
我遇到的问题是下拉列表中有两组值。 如果选择类型“A”,我希望用数据库中的值填充文本框,并且该文本框是只读的。 如果选择类型“B”,则该框将为空且可编辑。
实现这一点
我的原始代码是用 jsp/struts 编写的,我通过使用onchange="javascript:submit()"
重新加载页面来 ,但这有一个明显的缺点,即保存您所做的任何更改已经做了,这意味着你不能真正取消。
由于这种方法,我还遇到了服务器端验证的其他问题。
有没有一种方法可以使 jsp 页面在更改时重新加载,这样我就可以编写 javascript 来根据会话中保存的值来更改页面的外观。 这样,只有在正确填写页面并且服务器端验证将按设计工作时,才会调用保存/提交功能。
我知道这是 AJAX 擅长做的事情,但我会尽可能避免它。
the problem I have is that I have two sets of values in a drop down list. If type 'A' is selected I want a text box to be populated with a value from the database and be read only. If Type 'B' is selected the box is to be empty and editable.
My original code is written in jsp/struts and I have sort of achieved this by using
onchange="javascript:submit()"
to reload the page, but this has the obvious drawback of saving any changes you have made which means you can't really cancel.
I also have other problems with the serverside validation due to this method.
Is there a way of making a jsp page reload on change, that way I could write javascript to change the way the page looks according to the values held in the session. That way the save/submit function will only be called when the page has properly been filled out and the server side validation will work as designed.
I know that this is something that AJAX is good at doing but I am trying to avoid it if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我的朋友,AJAX 是您唯一的其他选择,除非在原始页面加载时加载文本框的所有其他可能值,这样您就不需要返回数据库。 好吧,您可以尝试将文本框放入 IFRAME 中,但与仅使用 AJAX 相比,这种方法可能会遇到更多问题。
AJAX is your only other option my friend, unless on the original page load you load all the other possible values of the Text Box so you don't need to go back to the database. Well, you could try putting the text box in an IFRAME, but you will probably run into more problems with that approach than just going with AJAX.
如果没有 AJAX,您所要求的将会很困难。 另一种选择(很难看)是将第二个列表框的所有可能值写入数组或字典等数据结构中。
然后编写一些 JavaScript,以便当用户从第一个列表框中进行选择时从数据结构中获取值。 为了完成这项工作并以跨浏览器的方式正确完成这项工作,您需要编写大量的 JavaScript,这比简单地使用 AJAX 困难得多。
Without AJAX what you are asking is going to be difficult. Another option (which is ugly) is to write out all possible values for the second list box into a data structure like an array or dictionary.
Then write some javascript to get the values from the data structure when the user selects from the first list box. The amount of javascript you will have to write to get this done and to do it correctly in a cross browser way will be much more difficult than simply using AJAX.
不知道为什么您会在当今世界尝试避免 AJAX,当今的 JS 库使它变得如此简单,不尝试就太疯狂了。
我只需替换按照文森特指出的那样编写的页面即可。 考虑到 4 年前的数据相对大小,我认为当时这对于应用程序来说是有意义的。 现在应用程序已经扩展,页面需要花费 30 秒以上的时间来重复解析数据结构(JS 写得不好?也许)。
我用一个非常简单的对 servlet 的 AJAX 调用替换了所有逻辑,该调用只是根据传递给它的内容返回第二个下拉列表的值的 JSON 响应,并且响应基本上是即时的。
祝你好运。
Not sure why you'd try to avoid AJAX in today's world, the JS libraries out there today make it so simple it's crazy not to try it out.
I just had to replace a page that was written as Vincent pointed out. I assume at the time it made sense for the app, given the relative size of the data 4 years ago. Now that the app has scaled though, the page was taking upwards of 30 seconds to parse the data structures repeatedly (poorly written JS? maybe).
I replaced all the logic with a very simple AJAX call to a servlet that simply returns a JSON response of values for the 2nd drop down based on what was passed to it and the response is basically instant.
Good luck to ya.
一种方法是更改表单的操作,以便将表单提交到与“保存”URL 不同的 URL。 这使您可以重新加载表单的某些方面并返回表单本身,而不是提交数据。
One way is to change the form's action so that you submit the form to a different url than the "save" url. This lets you reload certain aspects of the form and return to the form itself, instead of committing the data.
如果我理解正确,您需要一个下拉菜单 (
),具体取决于表单上方某处的选择(通常是复选框或单选按钮)?
在这种情况下,您可能需要在服务器上以不同的方式处理两种类型的输入,那么为什么不在表单区域中同时使用不同名称和 ID 的选择框和文本字段,并且其中一个隐藏(
display =无
)。 然后在选择更改时切换可见性。 在服务器上,您根据选择输入选择选择框或文本区域输入(除非您也禁用(disabled="disabled"
)它们,否则它们都会存在,我认为这是不必要的)。当然,如果您期望用户通常只需要文本输入,并且只需要几次,需要大量的列表; 最好使用ajax来检索列表。 但如果是相反的情况(您只是偶尔需要文本字段),正如我上面假设的那样,将两者都以初始形式呈现会更快。
如果下拉列表仅包含易于生成的数据,例如从现在到数百年前,使用 JavaScript 中的 for 循环生成数据客户端甚至可能会更快(需要更少的服务器带宽)。
If I understand you correctly, that you want either a dropdown (
<select>
) or a textfield (<input type="text">
) depending on a choice (typically a checkbox or radiobuttons) somewhere above in a form?I that case you may need to handle the two types of input differently on the server anyway, so why not have both the selectbox and textfield in the area of the form with different names and id and one of them hidden (
display = none
). Then toggle visibility when the choice changes. On the server you pick eiter the selectbox or textarea input (wich will both be present unless you disable (disabled="disabled"
) them too, wich I think is uneccesary) depending on the choice input.Of course if you expect that the user usually just need the text-input, and a few times only, needing a massive list; it would be better to use ajax to retrieve the list. But if it's the other way around (you need the text-field only occationally), as I assumed above, it will be faster to have both present in the initial form.
If the drop down only contain easily generateable data, like years from now to houndreds of years back it could even be much faster (requiring less bandwidth on the server) to generate the data client side using a for loop in Javascript.
我知道一个标签库可以解决您的问题:
AjaxTags。
我在我的 J2EE 项目中使用这个 taglib,并且将它集成到 Web 应用程序中非常简单。
该标签库为您提供了几个旨在在 jsp 文件中执行 AJAX 请求的标签。
以下是每个标签的描述: http://ajaxtags.sourceforge.net/usage.html
对您有帮助的标签是 ajax:select 标签。 它允许您填充依赖于其他字段的选择标记,而无需重新加载整个 jsp 页面。
如果您有更多相关信息,请询问我,我会尽力快速回答。
I know a taglib that can fit to your problem:
AjaxTags.
I use this taglib in my J2EE projects and it is very simple to integrate it into web applications.
This taglib give you several tags designed to execute AJAX request in your jsp files.
Here is the description of each tags: http://ajaxtags.sourceforge.net/usage.html
The tag which will help you is the ajax:select tag. It allows you to populate a select tag which depends on an other field without reloading the entire jsp page.
If you more informations about it, ask me and i'll try to answer quicky.
按照Strindhaug所说的,但是如果你需要动态数据:
你能让后端将JS写入页面,然后JS会根据需要改变表单吗? 后端可以传播一些用于描述等的变量,然后 JS 可以相应地更改/更新表单。 如果您对此不熟悉,像 jQuery 这样的库使这样的事情变得更容易、更交叉- 浏览器而不是自行开发(至少根据我的经验)。
在旁边:
如果您不使用 AJAX,因为它很难编码(因为我有一段时间没有使用 AJAX,因为我的第一次体验是从头开始并且不漂亮),正如其他人所说,像 MooTools 等使得现在变得非常容易。 而且,正确使用 AJAX 并不可耻。 它有一个坏名声,因为人们用它做了愚蠢的事情,但是如果您不能简单地将预制值写入表单或者您必须进行实时查找,那么这就是 AJAX 的正确用途之一。
Along the lines of what Strindhaug said, but if you need dynamic data:
Could you have the backend write JS into the page, and then the JS would change the form as required? The backend could propagate some variables for descriptions and such, and then the JS could change/update the form accordingly. If you aren't familiar with this, libs like jQuery make things like this easier and more cross-browser than rolling-your-own (at least in my experience).
Aside:
If you're not using AJAX because it was hard to code (as I didn't for a while because my first experience was from scratch and wasn't pretty), as others have said, libs like MooTools and such make it really easy now. Also, there is not shame in using AJAX properly. It has a bad rap because people do stupid things with it, but if you can't simply write premade values into the form or you have to do live look ups, this is one of AJAX's proper uses.