使用 jQuery 从隐藏字段中获取值

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

Get value from hidden field using jQuery

jquery

提问by X10nD

I have a <input type="hidden" value="" id='h_v' class='h_v'>Using jQuery I want to alert the user to this value .

我有一个<input type="hidden" value="" id='h_v' class='h_v'>Using jQuery 我想提醒用户注意这个值。

I am using

我在用

var hv = $('#h_v).text();
alert('x');

But its not working, any clues!

但它不起作用,任何线索!

回答by Sarfraz

Use val()instead of text()

使用val()代替text()

var hv = $('#h_v').val();
alert(hv);

You had these problems:

你有这些问题:

  • Single quotes was not closed
  • You were using text()for an input field
  • You were echoing xrather than variable hv
  • 单引号没有关闭
  • text()用于输入字段
  • 你是在回声x而不是变数hv

回答by dzida

This should work:

这应该有效:

var hv = $('#h_v').val();
alert(hv);

回答by MERT DO?AN

If you don't want to assign identifier to the hidden field; you can use name or class with selector like:

如果您不想为隐藏字段分配标识符;您可以将名称或类与选择器一起使用,例如:

$('input[name=hiddenfieldname]').val();

or with assigned class:

或指定班级:

$('input.hiddenfieldclass').val();

回答by MERT DO?AN

html

html

<input type="hidden" value="hidden value" id='h_v' class='h_v'>

js

js

var hv = $('#h_v').attr("value");
alert(hv);

example

例子

回答by Zeeshan Ali

var hiddenFieldID = "input[id$=" + hiddenField + "]";
var requiredVal= $(hiddenFieldID).val();

回答by Cris

var x = $('#h_v').val();
alert(x);

回答by Coronier

Closing the quotes in var hv = $('#h_v).text(); would help I guess

关闭引号 var hv = $('#h_v).text(); 会帮助我猜