设置<;选择>;Javascript中的值
我正试图用javascript设置a设置<;选择>;Javascript中的值,javascript,html,Javascript,Html,我正试图用javascript设置a的值,下面是我的代码 var div = document.createElement("DIV"); var select = document.createElement("SELECT"); var option_mon = document.createElement("OPTION"); option_mon.value = "monday"; var monday = document.createTextNode("Monday"); var
的值,下面是我的代码
var div = document.createElement("DIV");
var select = document.createElement("SELECT");
var option_mon = document.createElement("OPTION");
option_mon.value = "monday";
var monday = document.createTextNode("Monday");
var option_tue = document.createElement("OPTION");
option_tue.value = "tuesday";
var tuesday = document.createTextNode("Tuesday");
select.value = "tuesday";
option_mon.appendChild(monday);
option_tue.appendChild(tuesday);
select.appendChild(option_mon);
select.appendChild(option_tue);
div.appendChild(select);
但是,当我运行此命令时,我不认为星期二是选中的,星期一是选中的,因为它只是
中的第一个值,我如何设置
的值呢
select.value = "tuesday";
之后
select.appendChild(option_mon);
select.appendChild(option_tue);
由于您应该在将选项添加到选择后选择该值,请尝试此操作(在上一条语句之后,在选择中设置该值)
添加选项后,必须设置选择.value
。否则,没有可供选择的选项 试试这个:
var div = document.createElement("DIV");
var select = document.createElement("SELECT");
var option_mon = document.createElement("OPTION");
option_mon.value = "monday";
var monday = document.createTextNode("Monday");
var option_tue = document.createElement("OPTION");
option_tue.value = "tuesday";
option_tue.selected = "selected";
var tuesday = document.createTextNode("Tuesday");
option_mon.appendChild(monday);
option_tue.appendChild(tuesday);
select.appendChild(option_mon);
select.appendChild(option_tue);
div.appendChild(select);
未经测试,但应该有效
关键是:
option_tue.selected = "selected";
因为这就是我在HTML中要做的,不是吗?添加select.value=“星期二”代码>在选项后追加子项(星期二)代码>让它工作
检查您是否更新了工作代码:
var div = document.createElement("DIV");
var select = document.createElement("SELECT");
var option_mon = document.createElement("OPTION");
option_mon.value = "monday";
var monday = document.createTextNode("Monday");
var option_tue = document.createElement("OPTION");
option_tue.value = "tuesday";
var tuesday = document.createTextNode("Tuesday");
option_mon.appendChild(monday);
option_tue.appendChild(tuesday);
select.appendChild(option_mon);
select.appendChild(option_tue);
select.value = "tuesday";
div.appendChild(select);
document.getElementsByTagName('body')[0].appendChild(div);
var div=document.createElement(“div”);
var select=document.createElement(“select”);
var option_mon=document.createElement(“选项”);
选项_mon.value=“星期一”;
var monday=document.createTextNode(“星期一”);
var option_tue=document.createElement(“选项”);
选项_tue.value=“星期二”;
var星期二=document.createTextNode(“星期二”);
选择.value=“星期二”;
选项(星期一);;
方案二:儿童(星期二);
选择.appendChild(选项\u mon);
选择.appendChild(选项_tue);
select.setAttribute('id','selectMe')
div.appendChild(选择);
document.body.appendChild(选择)
var e=document.getElementById(“selectMe”);
e、 value=“星期二”;
函数getvalue(){
var e=document.getElementById(“selectMe”);
var strUser=e.options[e.selectedIndex].value;
document.getElementById(“p”).innerHTML=strUser;
}
函数setvalue(){
var e=document.getElementById(“selectMe”);
e、 value=“星期一”;
getvalue()
}
set valueget value
Putselect.value=“星期二”代码>在select中添加选项后,它应该会工作。op不是在做什么吗?噢。废话。忘了提了。嗨,你看到答案了吗?
var div = document.createElement("DIV");
var select = document.createElement("SELECT");
var option_mon = document.createElement("OPTION");
option_mon.value = "monday";
var monday = document.createTextNode("Monday");
var option_tue = document.createElement("OPTION");
option_tue.value = "tuesday";
var tuesday = document.createTextNode("Tuesday");
option_mon.appendChild(monday);
option_tue.appendChild(tuesday);
select.appendChild(option_mon);
select.appendChild(option_tue);
select.value = "tuesday";
div.appendChild(select);
document.getElementsByTagName('body')[0].appendChild(div);