在查询字符串中将隐藏字段从一个页面传递到另一页面
我想通过查询字符串将隐藏字段中的查询从一页传递到另一页。 谁能帮我理清逻辑吗?
I want to pass a query in a hidden filed from 1 page to another by querystring.
Can anyone help me out with the logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
花时间学习 jQuery 是值得的。它并不是很复杂,并且使得编写 javascript 变得更加容易。还有很多 jQuery 插件,例如 jquery.url。
此外,正如其他发帖者所建议的那样,如果您关心将隐藏字段的值显示给用户,您可能不希望将其放入查询字符串中。但是,如果数据存在于隐藏字段中,则用户只要愿意查看,就始终可以找到它。
如果您确实想将隐藏字段放入查询字符串中,然后通过非 jQuery javascript 提取它:
hiddenFieldPage.aspx
当用户执行此操作时,此表单会将用户带到processingPage.aspx?datum=someValue已提交。如果不需要同时提交其他内容,您也可以只使用普通链接。
或者,插入代码隐藏中的值:
processingPage.aspx
该脚本将弹出一个警告框,其中包含 URL 中的“datum”值 - 假设表单的方法设置为“GET” :
如果表单的方法设置为“POST”(通常在 ASP.NET 中如此),则“datum”将不会出现在查询字符串中,您必须再次将其放置在页面上
:第二页的隐藏值:
It's worth taking the time to learn jQuery. It's not very complicated, and it makes writing javascript much easier. There are also many jQuery plugins, such as jquery.url.
Also, as other posters have suggested, you may not wish to put the hidden field's value in the query string if you care about it being displayed to the user. However, if the data is present in a hidden field it will always be possible for a user to find it if they care to look.
If you really do want to put the hidden field in the query string and then extract it via non-jQuery javascript:
hiddenFieldPage.aspx
This form will take the user to processingPage.aspx?datum=someValue when it is submitted. You could probably also just use an ordinary link if nothing else needs to be submitted at the same time.
or, inserting the value from code-behind:
processingPage.aspx
This script will pop-up an alert box with the value of "datum" from the URL - assuming the form's method is set to "GET":
If the form's method was set to "POST" (as it usually would be in ASP.NET), then "datum" won't be in the query string and you'll have to place it on the page again:
To retrieve the hidden value on the second page:
您可以使用
action
参数轻松地在一个页面上提交指向另一页面的表单。例如,在 page1.aspx 内部添加以下内容:由于您使用“GET”而不是“POST”作为方法,因此您可能会使用 Javascript 来解析 URL 并获取传递的值。或者,您可以使用 ASPX 将“用户名”字段的值存储在页面上的其他位置。我不知道 ASPX(或 ASP,或任何 Microsoft 的东西),但如果您能找到一种方法来输出类似以下内容(并且正在使用 jQuery),它可能会满足您的要求。但老实说,听起来你正在做一些完全错误的事情。您能否修改您的问题,使其更具体地说明您试图实现的一般目标是什么?
You can easily submit a form on one page that points to another page using the
action
parameter. For instance, inside of page1.aspx put the following:Since you're using "GET" as the method instead of "POST", you could potentially use Javascript to parse the URL and get the value that was passed. Alternatively, you could use ASPX to store the value of the "username" field somewhere else on the page. I don't know ASPX (or ASP, or anything Microsoft really), but if you can find a way to output something like the following (and are using jQuery), it may do what you require. Honestly though, it sounds like you are going about something all wrong. Can you modify your question to be a bit more specific about what the general object is that you are attempting to accomplish?
假设您的意思是隐藏在 HTML 表单中,则提交表单时您的字段将与所有其他字段一起提交。如果您通过 GET 提交,那么您的“隐藏”字段将以纯文本形式显示在 URL 中。如果您不希望用户可以访问隐藏字段中的数据,请不要在该字段中放置可理解的值。
Assuming you mean hidden in the HTML form sense, your field will be submitted along with all the other fields when the form is submitted. If you are submitting via GET, then your "hidden" field will show up in plain text in the URL. If you don't want the data in the hidden field to be accessible to users, don't put an understandable value in that field.
如果您使用的是 aspx,则不需要使用 JavaScript 解析查询字符串,甚至不需要使用
page1.aspx:
page2.aspx:
上面会将名为
myHiddenClientValue
的客户端 JavaScript 变量设置为值 < POST 之后的代码>'myHiddenServerValue'。这可能是一个坏主意,因为如果
myHiddenServerField
包含单引号或换行符,则在page2.aspx
中的客户端上设置它可能会失败。 在客户端 JavaScript 中嵌入 ASP.NET 服务器变量 和 在客户端 JavaScript 中嵌入 ASP.NET 服务器变量,第 2 部分专门处理这些问题,并使用服务器端类来解决它们,确保写入客户端的值正确转义。If you are using aspx, you do not need to parse the query string using JavaScript, or even use
<form method="GET" ...>
. You can POST the form to the secondaspx
page, extract the value in C# or VB then write it to a client-side JavaScript variable. Something like this:page1.aspx:
page2.aspx:
The above would set the client-side JavaScript variable called
myHiddenClientValue
to a value of'myHiddenServerValue'
after the POST.This can be a bad idea because if
myHiddenServerField
contains single quotes or a newline character, then setting it on the client inpage2.aspx
can fail. Embedding ASP.NET Server Variables in Client JavaScript and Embedding ASP.NET Server Variables in Client JavaScript, Part 2 deals with specifically these issues, and solves them with a server-side class that ensures values being written to the client are escaped correctly.如果您在 HTML 表单上使用 method="get",则该表单中的任何隐藏输入都将转换为查询参数。
另请参阅杰里米·斯坦因的回答。
If you use method="get" on an HTML form then any hidden inputs in that form will be converted to query parameters.
See also Jeremy Stein's answer.