我正在使用 Symfony,我有很多具有不同 ID 和值的复选框,所以当我选中一个时,我想自动将其值(名称)插入到输入中,当我选中其他时将其值插入相同的输入:
<input type="checkbox" name="{{ent.username}}" value="{{ent.username}}">
谢谢大家
最佳答案
这不依赖于 Symphony。
jQuery:
$(function() { // when page loads
$("input[type='checkbox']").on("click",function() {
if (this.checked) $("#somefield").val(this.value);
});
});
纯 JS:
window.onload=function() { // when page loads
document.querySelectorAll("input[type='checkbox']").forEach(function() {
this.onclick=function() {
if (this.checked) document.getElementById("somefield").value=this.value;
}
}
}
关于javascript - 使用 jQuery 将复选框值(名称)插入到输入中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31730540/