javascript 无法在javascript中设置隐藏字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10529439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:11:33  来源:igfitidea点击:

Unable to set hidden field value in javascript

javascriptasp.nethidden-field

提问by Kings

I have defined a hidden filed in aspx:-

我在 aspx 中定义了一个隐藏的文件:-

<asp:HiddenField runat ="server" ID="hfBrand" value = "False" />

A combo-box control is calling a JavaScript function function:-

组合框控件正在调用 JavaScript 函数函数:-

<telerik:RadComboBox ID="Brands" runat="server"  
                onclientselectedindexchanged="LoadProperties"
                AutoPostBack="True">
</telerik:RadComboBox>

In that function , I am trying to set that hidden field value to "True":-

在该函数中,我试图将该隐藏字段值设置为“True”:-

function LoadProperties(sender, eventArgs)
{ 
var lblBrand = document.getElementById('<%= hfBrand.ClientID %>').value;
alert(lblBrand);
document.getElementById('<%= hfBrand.ClientID %>').value = "True";
alert(lblBrand)
}

QUESTION:- I can't set the hidden field value to TRUE. WHY it so?? and How to do that?

问题:- 我无法将隐藏字段值设置为 TRUE。为什么会这样??以及如何做到这一点?

UPDATE:- Here is the solution which got me right desired output:-

更新:-这是让我得到正确输出的解决方案:-

var lblBrand = document.getElementById('<%= hfBrand.ClientID %>').value;
alert(lblBrand);
lblBrand = "True";
alert (lblBrand);

Thnx to everyone who helped me.

感谢所有帮助过我的人。

回答by Steve

Your second alert is not referring to the hidden values new value. lblBrand value doesnt change after you have updated the hfBrand input value

您的第二个警报不是指隐藏值新值。更新 hfBrand 输入值后,lblBrand 值不会更改

function LoadProperties(sender, eventArgs)
{ 
var lblBrand = document.getElementById('<%= hfBrand.ClientID %>');
alert(lblBrand.value);
document.getElementById('<%= hfBrand.ClientID %>').value = "True";
alert(lblBrand.value)
}

Try this code!

试试这个代码!

I reckon it is setting it and you need to get the value of the input again...

我认为它正在设置它,您需要再次获取输入的值...

(if you wanna see the change, just temporarily change your hidden input to a normal textbox

(如果您想看到更改,只需暂时将隐藏的输入更改为普通文本框