从隐藏字段中获取价值 jQuery 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8643152/
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-08-27 10:48:53  来源:igfitidea点击:

Get Value from Hidden Field jQuery not working

jquerywebformshidden-field

提问by Suave Nti

Struck with a simple issue of hidden field like this:

遇到一个简单的隐藏字段问题,如下所示:

      <input id="hid" type="hidden" name="hid" runat="server"/>

trying to retrieve like this

试图像这样检索

      var rid = $("#hid").val();
      alert(rid);

But still am getting undefined value in the alert. Browser used : chrome

但仍然在警报中获得未定义的值。使用的浏览器:chrome

采纳答案by Pavan

Its working for me. Here is the JSFiddle. http://jsfiddle.net/P5r9N/

它为我工作。这是 JSFiddle。http://jsfiddle.net/P5r9N/

HTML

HTML

<input id="hid" type="hidden" name="hid" runat="server" value="working"/>

jQuery

jQuery

var rid = $("#hid").val(); 
alert(rid); 

回答by Connie

I had the same problem then realized I didn't have the idattribute of the hidden field set. After I added the field id, this worked fine

我遇到了同样的问题,然后意识到我没有id设置隐藏字段的属性。添加字段后id,这工作正常

alert($('#fieldid').val());

回答by Siva Shibhi

user978064 is correct. However, this is an alternative solution. I had this same issue and somewhat the HTML hidden control does show empty value. I know this is weird!

user978064 是正确的。但是,这是一种替代解决方案。我遇到了同样的问题,并且 HTML 隐藏控件确实显示了空值。我知道这很奇怪!

<input type="hidden" id="hid101" value="c5956aeb-96f3-4073-b130-62c8c7fbaca2">

jQuery("#hid101").val();- This returns empty value

jQuery("#hid101").val();- 这将返回空值

<input type="text" style="visibility:hidden" id="txt101" value="c5956aeb-96f3-4073-b130-62c8c7fbaca2">

jQuery("#txt101").val();- This returns expected value "c5956aeb-96f3-4073-b130-62c8c7fbaca2"

jQuery("#txt101").val();- 这将返回预期值“c5956aeb-96f3-4073-b130-62c8c7fbaca2”

This may help to someone!

这可能对某人有所帮助!

回答by vyshak

Try this it will work

试试这个它会工作

// By `ID`
alert($('input#hid').val());

// By `Name`
alert($('input[name=hid]').val());

// By `Type`
alert($('input[type=hidden]').val());

回答by user978064

Declare style="display:hidden"on the input type="text".

声明style="display:hidden"input type="text"

The same thing happen to me just now.

同样的事情刚刚发生在我身上。

回答by Mat41

This post was very helpful to me. For others who it also helps use type="text" style="display:none;" instead of

这篇文章对我很有帮助。对于其他也有帮助的人,请使用 type="text" style="display:none;" 代替