遗留 ASP.NET 1.1 与 jQuery 集成问题

发布于 2024-07-14 22:35:06 字数 951 浏览 7 评论 0原文

我正在开发一个用 VB.NET 为 ASP.NET 1.1 编写的旧 Web 应用程序。 一个特定页面有一个包含多个字段的表单。 为了响应下拉框更改值,我清除了多个字段,将多个下拉框重置为第一个选项,并将它们在 UI 中全部设置为“禁用”。 为此,我使用 jQuery。 我向所有这些字段添加一个 CSS 类,然后我的 jQuery 选择器如下所示:$("*.my-css-class")。 这是一些示例代码来解释。

var fields = $("*.fields");
if (some_condition) {
    fields.val("");
    fields.attr("selectedIndex", 0);
    fields.attr("disabled", "disabled");
}

UI 按预期更新以响应上述 js 代码,但是当我回发页面以响应按钮单击时,原始值仍然保留在与这些控件相关的服务器端。 例如,txtSomething 是具有 CSS 类“fields”的字段之一(因此它将被上面的 jQuery 选择器选中)。 用户在此文本框中键入“1234”并提交表单。 要么将同一页面发回自身并保留其值,要么我返回此页面并在服务器端预填充值(例如,用户单击摘要页面上的“编辑”按钮),因此控件 txtSomething 已初始化在值为“1234”的客户端上。 我的 jQuery 代码会清除用户在 UI 中看到的值,然后用户单击提交按钮。 如果我使用 jQuery 选择器询问该值,则该字段的值是一个空字符串。 当页面回发并且我正在单步执行代码(或使用此控件的值执行某些操作)时,它仍然是“1234”。

需要强调的一点是,这些值在提交一次后就会发送回浏览器。 因此,想象一下正在提交的表单,或者这些值在服务器端绑定或设置并输出到预填充的浏览器的任何情况(而不是使用默认值或空值输出到浏览器)。 如果我默认加载页面(空文本框),输入一些文本,然后触发 js 函数来清除这些字段,我输入的值永远不会到达服务器。

I am working on a legacy web application written in VB.NET for ASP.NET 1.1. One particular page has a form with a number of fields. In response to a drop-down box changing value, I am clearing a number of fields, resetting a number of drop-down boxes to the first option, and setting them all to "disabled" in the UI. To do this, I'm using jQuery. I add a CSS class to all of these fields, and then my jQuery selector is something like the following: $("*.my-css-class"). Here's some sample code to explain.

var fields = $("*.fields");
if (some_condition) {
    fields.val("");
    fields.attr("selectedIndex", 0);
    fields.attr("disabled", "disabled");
}

The UI updates as expected in response to the above js code, but when I post back the page in response to a button click, the original values still persist on the server-side related to these controls. For instance, txtSomething is one of the fields with a CSS class "fields" (so it will get selected by the above jQuery selector). The user types "1234" in this text box and submits the form. Either the same page is posted back to itself retaining its values, or I return to this page and prepopulate the values on the server-side (for example, the user clicks an Edit button on a summary page), so the control txtSomething is initialized on the client with the value "1234". My jQuery code clears the value as far as the user sees it in the UI, and then the user clicks a submit button. If I interrogate the value with a jQuery selector, the value of this field is an empty string. When the page is posted back and I'm stepping through the code (or doing something with the value of this control), it is still "1234".

A very important point to make is that these values are sent back to the browser after being submitted once. So, picture a form being submitted, or any case where these values are bound or set on the server-side and outputted to the browser pre-populated (as opposed to being output to the browser with default or empty values). If I load the page as default (empty text boxes), enter some text, and then trigger the js function to clear these fields, the value I typed never makes it to the server.

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

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

发布评论

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

评论(1

笑饮青盏花 2024-07-21 22:35:06

为什么需要禁用这些字段? 禁用控件可以使它们无法将值发布到服务器...至少当服务器端禁用 ASP.NET 控件时会发生这种情况。

更新1:无法怀疑它是否只是服务器端,所以我查了一下:) http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1 .. “在此示例中,INPUT 元素被禁用。因此,它无法接收用户输入,也不会随表单提交其值。”,所以我是对的,即使在客户端禁用它,它也不会发布该值。

why do you need to disable those fields? Disabling controls can make them not post values to the server ... at least that is what happens when an asp.net control is disabled server side.

Update 1: couldn't take having the doubt if it was only server side, so I looked it up :) http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1 ... "In this example, the INPUT element is disabled. Therefore, it cannot receive user input nor will its value be submitted with the form.", so I was right, even when disabling it client side it won't post the value

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