给定一系列带有唯一 ID 的 DIV,如下所示:
<div id="2988">
<div class="site">YaHoot.com</div>
<div class="logo">/images/yahootLogo.jpg</div>
//[and so on]
<input type="button" value="YaHoot" onclick="javascript:processMe('2988');" />
</div>
//[followed by bunches more divs repeating that structure]
我正在尝试编写一个单击处理程序,该处理程序可以将表单的隐藏字段与每个子 DIV 的内容进行映射。
换句话说,表单有一个隐藏字段:
<input type="hidden" name="logo">
当单击给定按钮时,应使用 /images/yahootLogo.jpg 对其进行评估。
谢谢
最佳答案
我喜欢为此使用 jQuery 的上下文功能,例如:
function processMe(id){
var logo = $(".logo", "#"+id).text();
$("input[name='logo']").val(logo);
}
关于javascript - 根据父 div 的子级设置输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3841757/