如何通过JS添加标签?

时间:2018-02-16 17:11:19

标签: javascript html

我有这个功能,可以为一天中的每个月创建不同的单选按钮。我添加了一个标签,每个按钮的默认值为1。但现在我有两个问题。

  1. 我无法再点击按钮了,我以前就可以了。在标签之前,我可以单击按钮并将值添加到我的MySql数据库。

  2. 当我更改月份时,新月份会在下方显示为最高月份。因此,如果我在2月份,我会得到28个按钮,当我换到3月时,我会得到31个以下。在功能刚添加按钮之前,我在更改月份时添加了3个按钮。

  3. 要获得下个月:

    function next() {
      if (showDate.getMonth() == 11) {
        showDate.setMonth( 0 );
        showDate.setFullYear( showDate.getFullYear()+1 );
      } else {
        showDate.setMonth( showDate.getMonth()+1 );
      }
      document.getElementById("displayingMonth").innerHTML = months[showDate.getMonth()];
      drawTable( showDate );
    }
    

    绘制表格:

    var showDate = new Date();
    var months = ["Januari", "Februari", "March", "April", "May", "June",
                  "July", "Augusti", "September", "October", "November", "December"];
    var weeks = ["Sunday","Monday","Tuseday","Wednesday","Thursday","Friday","Saturday"];
    
    function drawTable(forDate) {
      var daysInMonth = new Date(forDate.getFullYear(),forDate.getMonth()+1,0).getDate();
      var v = c +1;        
      var cellsToDraw = daysInMonth;
      var month = forDate.getMonth()+1;// non-index version of selected Month
      var newdate = forDate.getFullYear() + "-" + month;
      var table = document.getElementById("table");
      table.innerHTML = "";
      for (var r = 0; r < (daysInMonth / 7); r++) {
        var newRow = document.createElement("tr");
        table.appendChild(newRow);
        for (var c = 0; c < 31 && cellsToDraw > 0; c++) {
          var newCell = document.createElement("td");
          var input = document.createElement("input");
          var label = document.createElement("label");
          input.setAttribute("type", "radio");
          input.setAttribute("name", "day");
          label.appendChild(document.createTextNode("1"));
          newCell.appendChild(label);
          newRow.appendChild(newCell);
          cellsToDraw--;
        }
      }
    }
    

    上传代码:

    window.onload = function() {
        document.getElementById("displayingMonth").innerHTML = months[showDate.getMonth()];
        drawTable( showDate );
    };
    

    HTML

     <table id="table" cellspacing="0" cellpadding="0"   border-collapse="collapse";>
              <input id="newCell"type="hidden"name="day" value="">
      <h1 id="displayingMonth"></h1>
    

    所有的帮助都是适当的! :)

1 个答案:

答案 0 :(得分:0)

首先,您需要id来链接标签和单选按钮。我使用input

同时在labelinput.id = label.htmlFor = rando_id;上设置此项

rando_id只是一个随机生成的数字,最多5000个前缀为&#34; radio _&#34;。

最后,您没有在任何时候将单选按钮附加到屏幕上。我使用label.appendChild(input);

将其添加到标签元素中
  var rando_id = "radio_" + Math.floor(Math.random() * Math.floor(5000));
  var newCell = document.createElement("td");
  var input = document.createElement("input");
  var label = document.createElement("label");
  input.id = label.htmlFor = rando_id;
  input.setAttribute("type", "radio");
  input.setAttribute("name", "day");
  label.appendChild(document.createTextNode("1"));
  label.appendChild(input);

&#13;
&#13;
function next() {
  if (showDate.getMonth() == 11) {
    showDate.setMonth( 0 );
    showDate.setFullYear( showDate.getFullYear()+1 );
  } else {
    showDate.setMonth( showDate.getMonth()+1 );
  }
  document.getElementById("displayingMonth").innerHTML = months[showDate.getMonth()];
  drawTable( showDate );
}


var showDate = new Date();
var months = ["Januari", "Februari", "March", "April", "May", "June",
              "July", "Augusti", "September", "October", "November", "December"];
var weeks = ["Sunday","Monday","Tuseday","Wednesday","Thursday","Friday","Saturday"];

function drawTable(forDate) {
  var daysInMonth = new Date(forDate.getFullYear(),forDate.getMonth()+1,0).getDate();
  var v = c +1;        
  var cellsToDraw = daysInMonth;
  var month = forDate.getMonth()+1;// non-index version of selected Month
  var newdate = forDate.getFullYear() + "-" + month;
  var table = document.getElementById("table");
  table.innerHTML = "";
  for (var r = 0; r < (daysInMonth / 7); r++) {
    var newRow = document.createElement("tr");
    table.appendChild(newRow);
    for (var c = 0; c < 31 && cellsToDraw > 0; c++) {
      var rando_id = "radio_" + Math.floor(Math.random() * Math.floor(5000));
      var newCell = document.createElement("td");
      var input = document.createElement("input");
      var label = document.createElement("label");
      input.id = label.htmlFor = rando_id;
      input.setAttribute("type", "radio");
      input.setAttribute("name", "day");
      label.appendChild(document.createTextNode("1"));
      label.appendChild(input);
      newCell.appendChild(label);
      newRow.appendChild(newCell);
      cellsToDraw--;
    }
  }
}



    document.getElementById("displayingMonth").innerHTML = months[showDate.getMonth()];
    drawTable( showDate );
&#13;
<table id="table" cellspacing="0" cellpadding="0"   border-collapse="collapse";>
          <input id="newCell"type="hidden"name="day" value="">
  <h1 id="displayingMonth"></h1>
&#13;
&#13;
&#13;