如何使用jQuery从HTML隐藏字段中获取文本
How to get text from hidden field in HTML by using jQuery
我需要使用jQuery从隐藏字段中获取值。我的代码,我隐藏一些值如下:
(...)$.each(response, function (index, value) {
$('#notiContent').append(
$('<li>Nowa wiadomosc : ' + value.Subject +
' od ' + value.Receive + ' data ' + ToJavaScriptDate(value.Date)
+ ' </li>' + '<li style = "display: none;">' + value.IDMessage + '</li>'))
});(...)
和后面的值。IDMessage是隐藏的,我必须再次获取它的值,然后传递到另一个地方。代码如下:
(...)$("#notiContent").delegate('li', 'click', function () {
var type = $(this).val(); (...)
不幸的是,type
变量总是给出0。当我将.val()
更改为.text()
或.html().toString()
时,它可以工作,但我无法从隐藏字段中获得值。
我该怎么做?
试试这个,我想这就是你想要的。在本例中,我向您展示了如何从static
元素中获取hidden
field
values
,以及如何在dynamic
环境中获取隐藏字段值。
注意:当您想要从隐藏字段中分配和获取值时,最佳实践是使用 input
字段type="hidden"
试试下面两个例子。好运。
<html>
<head></head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<body>
<h2>Get the value from static element ---> Note : to get the hidden value, best practice is to use <span style="background-color:red;color:white;">input</span> field</h2>
<div id="notiContentone">
<li style="cursor:pointer;">Nowa Wiadomosc bla bla bla</li>
<input type="hidden" id="myhidden" value="give_your_value_here">
</div>
<br><br><br><hr>
<h2>Get the value from dynamic elements</h2>
<div id="notiContentwo"></div>
</body>
<script type="text/javascript">
//get the value from static element implement
$("div#notiContentone li").click(function(){// in here we choose the li element inside the notiContentone
var hiddenValue = $("#myhidden").val(); //here we get the assinged value for the hidden field.
alert(hiddenValue);
});
//get the value from dynamic elements
$(document).ready(function(){
var createelement_li = $('<li style="cursor:pointer;color:orange;background-color:pink;width:400px;">Nowa Wiadomsc bla bla bla </li>');
var createelement_hidden_input = $('<input type="hidden" id="myhiddentwo" value="dynamically_added_value_here">');
$("#notiContentwo").append(createelement_li);
$("#notiContentwo").append(createelement_hidden_input);
$("div#notiContentwo li").click(function(){
var gettheHiddenValue = $("#myhiddentwo").val();
alert(gettheHiddenValue);
});
});
</script>
</html>
相关文章:
- 从 jquery 动态输入字段获取值
- 角度形式 - 从包装器中的字段获取模型
- 使用Javascript从html字段获取年龄
- 通过ACF从WordPress中的重复字段获取Brightcove视频
- 按标签时间字段获取字段值,如下所示
- 如何在不提交表单的情况下从输入字段获取文件路径
- 使用jQuery从隐藏输入字段获取值的问题
- 如何从输入字段获取多个值并添加到文本区域
- 通过数组中的一个字段获取对象
- PHP和如何在点击连接到每个输入字段的按钮后从多个输入字段获取值
- 从输入字段获取图像信息
- 从隐藏字段获取值- JavaScript
- 通过php从innerhtml字段获取值
- 从多个文件上传字段获取文件名
- 在MVC中通过Jquery从隐藏字段获取多个值
- 从输入字段获取变量值,并在SELECT-WHERE语句中使用它
- Javascript:按特定字段获取所有对象
- 是否可以使用javascript从文件字段获取$_FILES["inputID"]["tm
- 动态 crm - 使用 JavaScript 从 CRM 中的日期和时间字段获取日期字符串值
- 如何使用angular.copy从一个输入字段获取值到另一个输入字段?