1
document.getElementById('text').innerHTML += "<td class='surveyquest'>5. Years of Services :</td></tr>";
document.getElementById('text').innerHTML += 
          "<tr>
              <td>
                 <select  name='services" + fields + "' title='Years of Services'>
                 var myDate = new Date(); 
                 var year = myDate.getFullYear(); 
                 for(var i = 2008; i < year+1; i++)
                 {
                      document.write('<option value=\"'+i+'\">'+i+'</option>');
                 }
                 </select>
              </td>
           </tr>";

问题是选择框出现如下: 在此处输入图像描述

我怎么解决这个问题?

4

6 回答 6

1
document.getElementById('text').innerHTML += "<td class='surveyquest'>5. Years of Services :</td></tr>";
var myDate = new Date();
var year = myDate.getFullYear() + 1;
var html = '';
for(var i = 2008; i < year; i++)
    html += '<option value="' + i + '">' + i + '</option>';
document.getElementById('text').innerHTML += "<tr><td><select  name='services" + fields + "' title='Years of Services'>" + html + "</select></td></tr>";
于 2013-08-01T06:32:58.520 回答
0

您必须使用 parseInt 或 parseFloat 或任何可以将您的数据读取为数值的东西,我认为这里 parseInt 会很好。

于 2013-08-01T06:32:04.447 回答
0

不要将javascript代码放在字符串中......它不会执行。

问题 :

<select  name='services" + fields + "' title='Years of Services'> <-- String is not terminated. It will take next line also as part of this string..

             var myDate = new Date(); <-- so, this line is part of string.
            var year = myDate.getFullYear(); <-- This line also
            for(var i = 2008; i < year+1; i++) <-- This line also
             {         <-- This line also
                  document.write('<option value=\"'+i+'\">'+i+'</option>'); <-- This line also
             }   <-- This line also
</select> <-- the string ends here

解决方案:

var comboHTML =  "<tr><td><select  name='services" + fields + "' title='Years of Services'>"


var myDate = new Date(); 
var year = myDate.getFullYear(); 

for(var i = 2008; i < year+1; i++){ 
     comboHTML+="<option value="+i+">"+i+"</option>" 
}

comboHTML+= "</select></td></tr>";

document.getElementById('text').innerHTML += comboHTML;
于 2013-08-01T06:32:32.523 回答
0

我认为 innerHTML 不能用于创建选择框。您可以使用下面的示例代码来动态创建一个选择框

function addSelect(row, cellIndex, id, sid, sname, value, list, width, align, className)
{
    var cell = row.insertCell(cellIndex);
    cell.setAttribute("width", width);
    cell.setAttribute("align", align);
    cell.setAttribute("class", className);
    if(id != "")
        cell.setAttribute("id", id);
    var groupId = document.createElement("select");
    groupId.setAttribute("id", sid);
    groupId.setAttribute("name", sname);
    groupId.setAttribute("value", value);
    var optionArray = list.split("/");
    groupId.options[groupId.options.length] = new Option('---Select---','---Select---');
    for(var a=0; a<optionArray.length; a++)
    {
        var option = optionArray[a].split(",");
        groupId.options[groupId.options.length] = new Option(option[1],option[0]);
    }
    //groupId.options[groupId.options.length] = new Option('text_1','value_1');
    cell.appendChild(groupId);      
}
于 2013-08-01T06:34:21.523 回答
0
<option value='"+i+"'>"+i+"</option>
document.write() not needed

更喜欢 appendChild 到 innerHTML

于 2013-08-01T06:35:26.407 回答
0

为什么在从 javascript 生成的字符串中使用 javascript?

像这样做:

document.getElementById('text').innerHTML += "5. 服务年限:";

var myDate = new Date(); 
var year = myDate.getFullYear(); 
var temp = "<tr><td><select  name='services" + fields + "' title='Years of Services'>";
for(var i = 2008; i < year+1; i++)
{
    temp += '<option value="'+i+'">'+i+'</option>';
}
temp += "</select></td></tr>";
document.getElementById('text').innerHTML += temp;

document.createElement('<tag name here>');或者用and来做element.appendChild(childElement)

像这样(我更喜欢这种方式):

var myDate = new Date(); 
var year = myDate.getFullYear();
var tr = document.createElement("tr");
var td = document.createElement("td");
var select = document.createElement("select");
select.name = "services" + fields;
select.title = "Years of Services";
for (var i=2008; i <= year; i++) {
    var option = document.createElement("option");
    option.value = i;
    option.innerHTML = i;
    select.appendChild(option);
}
td.appendChild(select);
tr.appendChild(td);
document.getElementById('text').appendChild(tr);
于 2013-08-01T06:52:08.553 回答