输入隐藏控件不会在回发之间保留其值

发布于 2024-10-04 01:59:13 字数 580 浏览 6 评论 0原文

我正在使用 ASP.NET MVC 制作网页。 我定义了以下隐藏输入:

<%=Html.Hidden("inputHiddenSelectedMenuId") %>

我在这个 js 函数中设置了它的值:

function SetSelectedMenu(id) {
     $('#inputHiddenSelectedMenuId').val(id);         
 }

在 js init 函数中进行回发后,我想使用隐藏输入中设置的值,但该值是字符串空。

$(document).ready(function() {

     $('div.nav > a').removeClass('active');
     var id = $('#inputHiddenSelectedMenuId').val();
     if (id != "") {
         $("#" + id).addClass('active');
     }         
 });

谁能暗示为什么会发生这种情况?

I'm making an webpage using ASP.NET MVC.
I have the following input hidden definied:

<%=Html.Hidden("inputHiddenSelectedMenuId") %>

And i set its value in this js function:

function SetSelectedMenu(id) {
     $('#inputHiddenSelectedMenuId').val(id);         
 }

After a make a postback in the js init function i want to use the value set in the input hidden but the value is string empty.

$(document).ready(function() {

     $('div.nav > a').removeClass('active');
     var id = $('#inputHiddenSelectedMenuId').val();
     if (id != "") {
         $("#" + id).addClass('active');
     }         
 });

Can anyone give a hint why this is happening?

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

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

发布评论

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

评论(1

萌化 2024-10-11 01:59:13

您正在尝试读取 JavaScript 中输入的值。当您单击表单上的按钮时,它会回发您的页面,并且每次加载页面时都会重新运行 javascript。如果您所做的只是读取 javascript 中输入的值,则无需执行回发。

$('#inputHiddenSelectedMenuId').bind('click', function ()
{
     var id = $('#inputHiddenSelectedMenuId').val();
     // do stuff with it.
});

单击功能将在没有回发的情况下执行。

现在,如果您尝试在发布后从 MVC 中读取隐藏字段的内容,那么这是一个不同的问题。您必须通过模型绑定从表单数据中提取它(或直接通过 Request.Form[] 集合读取它。

public ActionResult SomeActionToPostTo(int inputHiddenSelectedMenuId)
{
     //model binding should find the form field called inputHiddenSelectedMenuId and populate the argument in this method with it's value. If it's not an integer then just change the type of the argument to the appropriate type.
}

You are trying to read the value of the input in javascript. When you click a button on a form and it does a post back your page is being reloaded and the javascript re-runs every time the page is loaded. If all you are doing is reading the value of an input in javascript you do not need to perform a postback.

$('#inputHiddenSelectedMenuId').bind('click', function ()
{
     var id = $('#inputHiddenSelectedMenuId').val();
     // do stuff with it.
});

The click function will be performed without a postback.

Now, if you are trying to read the contents of the hidden field from within MVC after a post then this is a different issue. You will have to pull it from the form data through model binding (or reading it directly through the Request.Form[] collection.

public ActionResult SomeActionToPostTo(int inputHiddenSelectedMenuId)
{
     //model binding should find the form field called inputHiddenSelectedMenuId and populate the argument in this method with it's value. If it's not an integer then just change the type of the argument to the appropriate type.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文