简体   繁体   English

如何从按钮获取值到复选框

[英]How do I get the value from a button into a checkbox

I have this function where it creates a button with a certain value. 我有此功能,它会创建具有特定值的按钮。 In my html form I have a checkbox. 在我的html表单中,我有一个复选框。 I want the value from the buttons to be added in the checkbox. 我希望将按钮中的值添加到复选框中。 For some reason I am not able to figure it out.. 由于某种原因,我无法弄清楚。

 var showDate = new Date(); var months = ["Januari", "Februari", "March", "April", "May", "June","July", "Augusti", "September", "October", "November", "December"]; var theForm = '<form class="" action="insert.php" method="post"><input id="day"type="checkbox" name="day" value=""><br><input id="btn" type="submit" value="Send"></form>'; function drawTable(forDate) { var daysInMonth = new Date(forDate.getFullYear(),forDate.getMonth()+1,0).getDate(); var cellsToDraw = daysInMonth; var newdate = forDate.getFullYear() +"-"+ ("0"+ (forDate.getMonth() + 1)).slice(-2); var xy = document.getElementById("x"); xy.innerHTML = ""; for (var r = 0; r < (daysInMonth / 7); r++) { var newRow = document.createElement("tr"); xy.appendChild(newRow); for (var c = 0; c < 31 && cellsToDraw > 0; c++) { var day1 = ("0" + (c + 1)).slice(-2); var textarea = document.createElement("button"); textarea.setAttribute("placeholder", day1 ); textarea.setAttribute("class", "row"); newRow.appendChild(textarea); textarea.setAttribute("name", "day"); textarea.setAttribute("id", "day"); textarea.setAttribute("day", newdate + "-" + day1 ); textarea.setAttribute("value", newdate + "-" + day1); textarea.innerHTML = day1; var textarea1 = document.createElement("div"); textarea1.setAttribute("class", "content"); newRow.appendChild(textarea1); textarea1.innerHTML = theForm; cellsToDraw--; } } var acc = document.getElementsByClassName("row"); var i; for (i = 0; i < acc.length; i++) { acc[i].addEventListener("click", function() { var content = this.nextElementSibling; var active = checkIfAnyVisible(); if (active) { active.className = 'content'; active.style.maxHeight = null; } if (content.style.maxHeight){ content.style.maxHeight = null; content.className = 'content'; } else if (!checkIfAnyVisible()) { content.style.maxHeight = content.scrollHeight + "px"; content.style.width = "100%"; content.className = 'content active'; } }); } function checkIfAnyVisible() { for (var i=0; i<acc.length; i++) { if (acc[i].nextElementSibling.className.includes('active')) { return acc[i].nextElementSibling; } else { continue; } } return false; } } window.onload = function() { document.getElementById("displayingMonth").innerHTML = months[showDate.getMonth()] + " " + showDate.getFullYear(); drawTable(showDate); }; function daysInMonth(month, year) { var days; switch (month) { case 1: var leapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); days = leapYear ? 29 : 28; break; case 3: case 5: case 8: case 10: days = 30; break; default: days = 31; } return days; } 
  .row { transition: 0.4s; } .content { max-height: 0; transition: max-height 0.2s ease-out; overflow: hidden; width: 98.3%; } #x{ width: 75%; } #displayingMonth { } 
  <h1 id="displayingMonth"></h1><br> <div id="x" cellspacing="0" cellpadding="0" border-collapse="collapse"></div> 

To add a value to a checkbox, you can use the simple .value = myvalue . 要将值添加到复选框,可以使用简单的.value = myvalue

Instead of this: 代替这个:
textarea.setAttribute("value", newdate + "-" + day1);

Use this: 用这个:
textarea.value = newdate + "-" + day1

but first, make sure that your variable textarea is beign correctly selected. 但首先,请确保正确选择可变textarea I don't know if you are using jQuery, but with pure JS, you will select your checkbox with: 我不知道您是否使用jQuery,但是使用纯JS,您将选择以下复选框:
document.getElementById('day')

EDIT 编辑
you had a bad mistake with the ids , the way you were doing you were setting the same ID to many elements, what is wrong, IDs must be unique! 您在使用ids犯了一个严重的错误,在您执行操作的过程中,您为许多元素设置了相同的ID,这是错误的,ID必须是唯一的! So i adjusted this. 所以我调整了这个。

The code: ( look for comments inside the JS code ) 代码:( 在JS代码中查找注释

 var showDate = new Date(); var months = ["Januari", "Februari", "March", "April", "May", "June","July", "Augusti", "September", "October", "November", "December"]; var theForm = '<form class="" action="insert.php" method="post"><input type="checkbox" name="day" value=""><br><input id="btn" type="submit" value="Send"></form>'; function drawTable(forDate) { var daysInMonth = new Date(forDate.getFullYear(),forDate.getMonth()+1,0).getDate(); var cellsToDraw = daysInMonth; var newdate = forDate.getFullYear() +"-"+ ("0"+ (forDate.getMonth() + 1)).slice(-2); var xy = document.getElementById("x"); xy.innerHTML = ""; for (var r = 0; r < (daysInMonth / 7); r++) { var newRow = document.createElement("tr"); xy.appendChild(newRow); for (var c = 0; c < 31 && cellsToDraw > 0; c++) { //new function to get day var day1 = getCorrectDay(c); var textarea = document.createElement("button"); textarea.setAttribute("placeholder", day1 ); textarea.setAttribute("class", "row"); newRow.appendChild(textarea); textarea.setAttribute("name", "day"); //textarea.setAttribute("id", "day"); --> REMOVE THIS, ID must be unique textarea.setAttribute("day", newdate + "-" + day1 ); textarea.value = newdate + "-" + day1; textarea.innerHTML = day1; var textarea1 = document.createElement("div"); textarea1.setAttribute("class", "content"); newRow.appendChild(textarea1); textarea1.innerHTML = theForm; //Lets find the checkbox var myCheckbox = textarea1.querySelector('input[type="checkbox"]'); myCheckbox.value = newdate + "-" + day1; //setting the value console.log(myCheckbox); //Logging to see if value is there cellsToDraw--; } } var acc = document.getElementsByClassName("row"); var i; for (i = 0; i < acc.length; i++) { acc[i].addEventListener("click", function() { var content = this.nextElementSibling; var active = checkIfAnyVisible(); if (active) { active.className = 'content'; active.style.maxHeight = null; } if (content.style.maxHeight){ content.style.maxHeight = null; content.className = 'content'; } else if (!checkIfAnyVisible()) { content.style.maxHeight = content.scrollHeight + "px"; content.style.width = "100%"; content.className = 'content active'; } }); } function checkIfAnyVisible() { for (var i=0; i<acc.length; i++) { if (acc[i].nextElementSibling.className.includes('active')) { return acc[i].nextElementSibling; } else { continue; } } return false; } } window.onload = function() { document.getElementById("displayingMonth").innerHTML = months[showDate.getMonth()] + " " + showDate.getFullYear(); drawTable(showDate); }; function daysInMonth(month, year) { var days; switch (month) { case 1: var leapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); days = leapYear ? 29 : 28; break; case 3: case 5: case 8: case 10: days = 30; break; default: days = 31; } return days; } //Created a class to get the day based on each loop iteration function getCorrectDay(idx){ var day = (idx + 1).toString(); if (day.length < 2){ day = '0' + day; } return day; } 
 .row { transition: 0.4s; } .content { max-height: 0; transition: max-height 0.2s ease-out; overflow: hidden; width: 98.3%; } #x{ width: 75%; } #displayingMonth { } 
 <h1 id="displayingMonth"></h1><br> <div id="x" cellspacing="0" cellpadding="0" border-collapse="collapse"></div> 

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

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