javascript - 使用 jQuery 将复选框值(名称)插入到输入中

标签 javascript jquery html symfony checkbox

我正在使用 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/

相关文章:

javascript - 正确更新div中的数据AJAX(ASP.NET MVC)

javascript - SAPUI5/Javascript 如何将二进制数据转换为可读格式并下载为 PDF

html - 如何将 3 个 block (span、span、input)放置在垂直对齐的一行中?

javascript - 旋转 html Canvas 线条图案

html - 在纯 css 3d 中创建圆柱形状

javascript - 检查 vendor Ex 的正确方法。 webkitRequestAnimationFrame || mozRequestAnimationFrame

javascript - iOS 7 :UIWebView (Safari) crashes due to memory overflow

javascript - 在母版页上有选择地调用 css

javascript - 如何取消设置/取消多个 Fx.Morph

jQuery Accordion 的 JavaScript 关闭问题(我相信)