jQuery 如何在jquery中分配,清除和获取隐藏字段的值?

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

How to assign, clear and get value of a hidden field in jquery?

jqueryhtml

提问by ACP

Consider a hiddenfield in the page HfId

考虑页面中的隐藏字段 HfId

<input type="hidden" id="HfId"/>

How to assign,clear and get value of a hidden field in jquery? ANy suggestion...

如何在jquery中分配,清除和获取隐藏字段的值?任何建议...

回答by Bobby

The Element Selectorof jQuery is exactly designed for this. It allows you to select all elements which fit the given name, id or class:

jQuery的元素选择器正是为此而设计的。它允许您选择适合给定名称、ID 或类的所有元素:

$("a")       // Get all a-elements
$("#id")     // Get all elements with the id "id"
$(".class")  // Get all elements with the class "class"

The visibility or other properties of the element does not interfere with that, as it is still present in the DOM of the document.

元素的可见性或其他属性不会干扰它,因为它仍然存在于文档的 DOM 中。

Fields have the overloaded function val(), val($value)which allows you to get and set the value of the field:

字段具有重载函数val()val($value)它允许您获取和设置字段的值:

$val = $("#HfId").val();  // Get
$("#HfId").val("");       // Clear
$("#HfId").val($newVal);  // Set

回答by svinto

$("#HfId").val("pony");
var x = $("#HfId").val();