我的代码如下:
const pagedata = {
name: "Value for name",
email: "Value for email",
};
$(".fillvalfromvar").val(pagedata[$(this).attr("id")]);
我需要填充所有具有以下元素的元素fillvalfromvar
class 具有元素 id 指向的变量值。例如 HTML 如下:
<input id="name" class="fillvalfromvar" type="text">
<input id="email" class="fillvalfromvar" type="text">
我想填补这些val
s with pagedata["name"]
and pagedata["email"]
, 分别。
But the this
value 不指向原始元素。我应该用什么来代替?
使用以下语法接受函数作为参数
$('.fillvalfromvar').val( function(){
return pagedata[ this.id ];
});
(假设那些input
元素有fillvalfromvar
class)
或者你可以使用.each()
method
$('.fillvalfromvar').each( function(){
this.value = pagedata[ this.id ];
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)