Tetbox 值未更新?

发布于 2024-12-04 12:34:33 字数 347 浏览 0 评论 0原文

我有一个已禁用的 asp.net 文本框,我正在使用 JQuery 在客户端设置文本框的值。

$('[id$=txtCity]').val('Naperville');

但问题是当我尝试保存数据时。如果我说 txtCity.Text 它不会给我在客户端设置的值。

文本框被禁用,在使用 JQuery 设置值后,我看到客户端文本框中反映的值。但我不确定为什么它没有反映在服务器端。

我发现 disabled 的 TextBox 将不会在回发时随表单一起提交。如果这是真的,我如何提交这些值并在服务器端的代码中获取它。

请帮忙。感谢并感谢您的反馈。

I have an asp.net textbox which is disabled and I am setting the value for the textbox on client side using JQuery.

$('[id$=txtCity]').val('Naperville');

But the problem is when I am trying to save the data. If I say txtCity.Text it is not giving me the value that I set in the client side.

The textbox is disabled and I am seeing the value reflected in the textbox on the client side after the value is set using JQuery. But I am not sure why it is not reflected on the server side.

I found that TextBox with disabled will not be submitted with the form on postback. If that is true, how can I submit those values and get that on server side in the code behind.

Please help. Thanks and appreciate your feedback.

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

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

发布评论

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

评论(2

迷爱 2024-12-11 12:34:33

您始终可以在发布之前“取消禁用”该字段。例如,

$('form').submit(function() {
  $(this).find(':input:disabled').removeAttr('disabled');
});

当然,根据 Knix 的回答,使用只读字段也可能是一个可行的解决方案。


Edit: just read your comment on Knix's answer, so it sounds like dynamically removing the disabled attribute before posting may be the way to go.

You can always "un-disable" the field before posting. E.g.,

$('form').submit(function() {
  $(this).find(':input:disabled').removeAttr('disabled');
});

And of course using readonly fields, as per Knix's answer, may be a viable solution as well.


Edit: just read your comment on Knix's answer, so it sounds like dynamically removing the disabled attribute before posting may be the way to go.

江心雾 2024-12-11 12:34:33

我认为禁用的字段不会被发布。

也许您应该尝试 readonly 字段:http://www.w3schools.com /tags/att_input_readonly.asp

<input id="firstName" name="firstName" value="hello world" READONLY>

您始终可以使用 CSS 使只读字段显示为禁用 =)

编辑:

好的..如果您无法执行只读,那么如何使用 AJAX 来发布并直接传递数据,因为你说你可以在客户端访问它:

$.ajax({
      url: "yourForm.php",  // sorry I used PHP in this example 
      type: "POST",
      data: { txtCity : $('#txtCity').val() },
      success: function(msg){
         alert(msg);
      }
   }
)

EDIT2:

填充隐藏字段并在服务器端使用该值怎么样,因为你不能使用只读字段或使用 AJAX?

$('#hiddenField').val('Naperville');

由于此字段是隐藏的,因此用户不会知道其中的区别 =)

EDIT3:

OP 使用正确的服务器端语法来处理只读输入:

TextBox1.Attributes.Add("readonly","readonly")

I don't think disabled fields will get posted.

Maybe you should try readonly field: http://www.w3schools.com/tags/att_input_readonly.asp

<input id="firstName" name="firstName" value="hello world" READONLY>

You can always use CSS to make the readonly field appear disabled =)

EDIT:

Ok..if you cannot do the readonly, how about using AJAX to post and directly passing over the data since you say you can access it on the client side:

$.ajax({
      url: "yourForm.php",  // sorry I used PHP in this example 
      type: "POST",
      data: { txtCity : $('#txtCity').val() },
      success: function(msg){
         alert(msg);
      }
   }
)

EDIT2:

How about populating a hidden field and using that value on the server-side since you cannot use readonly fields or use AJAX?

$('#hiddenField').val('Naperville');

Since this field is hidden, the user would not know the difference =)

EDIT3:

OP got this to work using correct server-side syntax for the readonly inputs:

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