Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 查找元素并设置值_Jquery - Fatal编程技术网

Jquery 查找元素并设置值

Jquery 查找元素并设置值,jquery,Jquery,我有一个id为hover-1的div元素,在div中我有一个隐藏的表单元素,在层次结构中有类名text\u page\u title。 我在一个变量中动态地获得了父div元素id,现在我想找到类名为text\u page\u title的隐藏元素,并将其值设置为“foo” 结构如下: <div id="hover-1"> <input type="hidden" class="text_page_title"> </div> <div id="hover

我有一个id为
hover-1
的div元素,在div中我有一个隐藏的表单元素,在层次结构中有类名
text\u page\u title
。 我在一个变量中动态地获得了父div元素id,现在我想找到类名为
text\u page\u title
的隐藏元素,并将其值设置为“foo”

结构如下:

<div id="hover-1">
<input type="hidden" class="text_page_title">
</div>
<div id="hover-2">
<input type="hidden" class="text_page_title">
</div>

但是它不起作用,我遗漏了什么?

假设
parentId
是包含id字符串(“hover-1”)的变量名,您应该像这样重写您的问题:

$('#'+parentId).find('input.text_page_title').val('foo');
使用此代码:

 $("#hover-1").find(".text_page_title").val('foo');

如果$parentId是父Id:

$('#'+$parentId).find('input.text_page_title:hidden').val('foo');
您将确保只获得隐藏的输入

此操作有效:

$("#hover-1").find('input.text_page_title').val('foo'); // or $("#hover-2")
如果希望在变量中包含父id:

var parentId = $("#hover-1"); // or $("#hover-2");
parentId.find('input.text_page_title').val('foo');

这应该行得通。我猜您在
$parentId
中有
id
,但没有
,所以请尝试
$(“#“+$parentId)
$(“#“+$parentId”).children(.text\u page\u title”).attr(“value”,“foo”)attr('value','foo');是无用的,因为它是一个输入-我们可以使用val()
var parentId = $("#hover-1"); // or $("#hover-2");
parentId.find('input.text_page_title').val('foo');