简体   繁体   English

javascript如何写选择标签?

[英]how to write select tag with javascript?

I have a website in which I am writing html code with javascript because I want to load everything into javascript code so that user won't feel any delay.But I am unable to implement "date of birth" part with javascript.Here is my code. 我有一个网站,我正在用JavaScript编写html代码,因为我想将所有内容加载到javascript代码中,这样用户就不会感到任何延迟。但是我无法使用javascript实现“出生日期”部分。码。

function getBasicProfileEditBody()
{
    var basicProfileEditBody = "<div>"+
                                    "<div>"+
                                        "<span>gender</span><span>:</span><span><input type='radio' name='gender' value='female' />female<input type='radio' name='gender' value='male' />male"+
                                    "</div>"+
                                    "<div>"+
                                        "<span>date of birth</span><span>:</span>"+
                                        "<span>"+
                                            "<select id='dateOfBirth'>"+

                                            +"</select>"+
                                        "</span>"+
                                    "</div>"+
                                    "<div>"+
                                        "<span>my strengths</span><span>:</span><span></span>"+
                                    "</div>"+
                                    "<div>"+
                                        "<span>my weaknesses</span><span>:</span><span></span>"+
                                    "</div>"+
                                    "<div>"+
                                        "<span>persons i like</span><span>:</span><span></span>"+
                                    "</div>"+                   
                                    "<div>"+
                                        "<span>things i like in others</span><span>:</span><span></span>"+
                                    "</div>"+
                                    "<div>"+
                                        "<span>things i don't like in others</span><span>:</span><span></span>"+
                                    "</div>"+
                                    "<div>"+
                                        "<span>my friends</span><span>:</span><span></span>"+
                                    "</div>"+
                                    "<div>"+
                                        "<span><input type='button' value='edit' id='aboutProfileEdit' /></span><span><input type='button' value='cancel' id='aboutProfileCancel' /></span>"+
                                    "</div>";
return basicProfileEditBody;
} 

In this style if I have to write select tag for dates it is ok but how about select tag for years,it would be a burden to me?wouldn't it be?How to implement "date of birth" in this style? 在这种样式中,如果我必须为日期写选择标记是可以的,但多年的选择标记又如何呢?这对我来说是一个负担吗?不是吗?如何以这种样式实现“出生日期”?

To answer your question, try following way: 要回答您的问题,请尝试以下方式:

First step: Replace the source code "<select id='dateOfBirth'>"+</select>" by "div id='dateOfBirth'>"+</div>" , because you need three select boxes. 第一步:将源代码"<select id='dateOfBirth'>"+</select>"替换为"div id='dateOfBirth'>"+</div>" ,因为您需要三个选择框。

Second step: Add some code to create each entries more or less automatically: 第二步:添加一些代码以或多或少地自动创建每个条目:

function getBasicProfileEditBody()
{
  ...

  var startBirthYearRange = 1970;
  var d = new Date();
  var endBirthYearRange = d.getFullYear();
  var yearEntries = endBirthYearRange - startbirthYear;
  var selectBoxYearOfBirth = "<select id='yearOfBirth'>";
  for (index = 0; index < yearEntries; index++)
  {
    selectBoxYearOfBirth += "<option>" + startBirthYearRange + index) + "</option>";
  }
  selectBoxYearOfBirth += "</select>";

  var birthMonth = new Array();
  birthMonth.push("1@January");
  birthMonth.push("2@February");
  birthMonth.push("3@March");
  ...
  birthMonth.push("12@December");
  var selectBoxMonthOfBirth = "<select id='monthOfBirth'>";
  for (index = 0; index < 12; index++)
  {
   var itemArry = birthMonth[index].split("@");
   selectBoxMonthOfBirth = += "<option value='" + itemArry[0] + "'>" +
                              itemArry[1] + "</option>";
  }
  selectBoxYearOfBirth += "</select>";

  var selectBoxDayOfBirth = = "<select id='dayOfBirth'>";
  for (index = 0; index < 31; index++)
  {
    selectBoxDayOfBirth = += "<option>" + (index + 1) + "</option>";
  )
  selectBoxDayOfBirth += "</select>";

  //Then apped the new three variables to var 'basicProfileEditBody'
  ...
}

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

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