简体   繁体   English

如何将 label 动态添加到复选框

[英]How can I add a label to a checkbox dynamically

I want my checkboxes to look like this:我希望我的复选框看起来像这样:

<input type="checkbox"name="id">Check 1</input>
<input type="checkbox"name="id">Check 2</input>
<input type="checkbox"name="id">Check 3</input>

How can I add some labels?如何添加一些标签?

My code:我的代码:

      response.forEach(row => {
         var checkbox = document.createElement('input');
         checkbox.type = "checkbox";
         checkbox.name =  model.toLowerCase()+"_id";
         checkbox.value = row.id;
         control.appendChild( checkbox);
      });

A label in html is a seperate HTML element: html 中的 label 是一个单独的 HTML 元素:

 <label for="my_checkbox_1">Checkbox 1</label> <input type="checkbox" id="my_checkbox_1"/> <br> <label for="my_checkbox_2">Checkbox 2</label> <input type="checkbox" id="my_checkbox_2"/> <br> <label for="my_checkbox_3">Checkbox 3</label> <input type="checkbox" id="my_checkbox_3"/>

Try adding something like this to your function:尝试将这样的内容添加到您的 function 中:

var label = document.createElement('label');
label.htmlFor = "ID_OF_CHECKBOX_HERE";
label.innerHTML = "LABEL_TEXT_HERE";

<input /> tags are self-closing. <input />标签是自动关闭的。 To add a label to a checkbox, you need to wrap it inside that label:要将 label 添加到复选框,您需要将其包装在 label 内:

<label><input type="checkbox" name="id"> Check 3</label>

Here is a demo:这是一个演示:

 var response = [{id: 1, name: "Check 1"}, {id: 2, name: "Check 2"}, {id: 2, name: "Check 3"}]; var model = "foo"; response.forEach(row => { // Create a label var label = document.createElement('label'); // Create a checkbox var checkbox = document.createElement('input'); checkbox.type = "checkbox"; checkbox.name = model.toLowerCase() + "_id"; checkbox.value = row.id; // Append the checkbox to the label label.appendChild(checkbox); // Append the label text to the label label.appendChild( document.createTextNode(row.name) ); // Append the label to the control area control.appendChild(label); });
 label { display: block; }
 <div id="control"></div>

There are several ways to do what you want有几种方法可以做你想做的事

Please take a look at the following example请看下面的例子

Using Template literals使用模板文字

 const response = [ { id: 1, }, { id: 2, }, { id: 3, }, { id: 4, }, ]; const inputs = response.map( ({ id }) => `<label> <input type="checkbox" name="${id}" /> Check ${id} </label>` ).join(""); document.querySelector("#root").innerHTML = inputs;
 <div id="root"></div>

Using <template>使用<template>

 const response = [ { id: 1, }, { id: 2, }, { id: 3, }, { id: 4, }, ]; const root = document.querySelector("div#root"); const template = document.querySelector("#input-template"); response.forEach(({ id }) => { const clone = template.content.cloneNode(true); clone.querySelector('input[type="checkbox"]').setAttribute("name", id); clone.querySelector("span").textContent = `Check ${id}`; root.appendChild(clone); });
 <div id="root"></div> <template id="input-template"> <label> <input type="checkbox" name="" id="" /> <span></span> </label> </template>

The problem with the DOM API is that it can quickly become very verbose and repetitive, and the sum of this makes it difficult to maintain. DOM API 的问题在于它很快就会变得非常冗长和重复,而这些总和使其难以维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM