在查询字符串中将隐藏字段从一个页面传递到另一页面

发布于 2024-08-05 12:53:57 字数 46 浏览 0 评论 0原文

我想通过查询字符串将隐藏字段中的查询从一页传递到另一页。 谁能帮我理清逻辑吗?

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 技术交流群。

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

发布评论

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

评论(6

陌上青苔 2024-08-12 12:53:57

花时间学习 jQuery 是值得的。它并不是很复杂,并且使得编写 javascript 变得更加容易。还有很多 jQuery 插件,例如 jquery.url

此外,正如其他发帖者所建议的那样,如果您关心将隐藏字段的值显示给用户,您可能不希望将其放入查询字符串中。但是,如果数据存在于隐藏字段中,则用户只要愿意查看,就始终可以找到它。

如果您确实想将隐藏字段放入查询字符串中,然后通过非 jQuery javascript 提取它:

hiddenFieldPage.aspx

当用户执行此操作时,此表单会将用户带到processingPage.aspx?datum=someValue已提交。如果不需要同时提交其他内容,您也可以只使用普通链接。

<form method="GET" action="processingPage.aspx">
    <input type="hidden" name="datum" value="someValue">
    <input type="submit">
</form>

或者,插入代码隐藏中的值:

RegisterHiddenField("datum", "someValue");

processingPage.aspx

该脚本将弹出一个警告框,其中包含 URL 中的“datum”值 - 假设表单的方法设置为“GET” :

    <script type="text/javascript">

        function getUrlParam( key ) {

            // Get the query and split it into its constituent params
            var query = window.location.search.substring(1);
            var params = query.split('&');

            // Loop through the params till we find the one we want
            for( var i in params ) { 
                var keyValue = params[i].split('=');
                if( key == keyValue[0] ) {
                    return keyValue[1];
                }
            }

            // Didn't find it, so return null
            return null;
        }

        alert( getUrlParam("datum") );
    </script>

如果表单的方法设置为“POST”(通常在 ASP.NET 中如此),则“datum”将不会出现在查询字符串中,您必须再次将其放置在页面上

RegisterHiddenField( "datum", Request.Form["datum"] );

:第二页的隐藏值:

var datum = document.Form1.item("datum").value;
alert( 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.

<form method="GET" action="processingPage.aspx">
    <input type="hidden" name="datum" value="someValue">
    <input type="submit">
</form>

or, inserting the value from code-behind:

RegisterHiddenField("datum", "someValue");

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":

    <script type="text/javascript">

        function getUrlParam( key ) {

            // Get the query and split it into its constituent params
            var query = window.location.search.substring(1);
            var params = query.split('&');

            // Loop through the params till we find the one we want
            for( var i in params ) { 
                var keyValue = params[i].split('=');
                if( key == keyValue[0] ) {
                    return keyValue[1];
                }
            }

            // Didn't find it, so return null
            return null;
        }

        alert( getUrlParam("datum") );
    </script>

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:

RegisterHiddenField( "datum", Request.Form["datum"] );

To retrieve the hidden value on the second page:

var datum = document.Form1.item("datum").value;
alert( datum );
懒的傷心 2024-08-12 12:53:57

您可以使用 action 参数轻松地在一个页面上提交指向另一页面的表单。例如,在 page1.aspx 内部添加以下内容:

<form action="page2.aspx" method="GET">
  <input type="hidden" name="username" value="joenobody" />
  <input type="submit" />
</form>

由于您使用“GET”而不是“POST”作为方法,因此您可能会使用 Javascript 来解析 URL 并获取传递的值。或者,您可以使用 ASPX 将“用户名”字段的值存储在页面上的其他位置。我不知道 ASPX(或 ASP,或任何 Microsoft 的东西),但如果您能找到一种方法来输出类似以下内容(并且正在使用 jQuery),它可能会满足您的要求。但老实说,听起来你正在做一些完全错误的事情。您能否修改您的问题,使其更具体地说明您试图实现的一般目标是什么?

<div id="some_div"><%= Request.form("username") %></div>

<script type='text/javascript'>
  var value_needed = $('#some_div').html();
</script>

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:

<form action="page2.aspx" method="GET">
  <input type="hidden" name="username" value="joenobody" />
  <input type="submit" />
</form>

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?

<div id="some_div"><%= Request.form("username") %></div>

<script type='text/javascript'>
  var value_needed = $('#some_div').html();
</script>
扮仙女 2024-08-12 12:53:57
<form method="get">
<form method="get">
人海汹涌 2024-08-12 12:53:57

假设您的意思是隐藏在 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.

谈下烟灰 2024-08-12 12:53:57

如果您使用的是 aspx,则不需要使用 JavaScript 解析查询字符串,甚至不需要使用

。您可以将表单发布到第二个 aspx 页面,提取 C# 或 VB 中的值,然后将其写入客户端 JavaScript 变量。像这样的内容:

page1.aspx:

<form method="POST" action="page2.aspx">
    <input type="hidden" name="myHiddenServerField" value="myHiddenServerValue">
    <input type="submit">
</form>

page2.aspx:

<script type="text/javascript">
var myHiddenClientValue = '<%= Request.Form['myHiddenServerField']; %>';
</script>

上面会将名为 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 second aspx page, extract the value in C# or VB then write it to a client-side JavaScript variable. Something like this:

page1.aspx:

<form method="POST" action="page2.aspx">
    <input type="hidden" name="myHiddenServerField" value="myHiddenServerValue">
    <input type="submit">
</form>

page2.aspx:

<script type="text/javascript">
var myHiddenClientValue = '<%= Request.Form['myHiddenServerField']; %>';
</script>

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 in page2.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.

感性 2024-08-12 12:53:57

如果您在 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.

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