使用JavaScript的createElement创建新的元素出现了问题

lioshey 2015-10-02 05:52:43
<script type="text/javascript">
var count=5;
function add()
{
var tr= document.createElement('tr');//创建行

var table = document.getElementById('tb');//获得table对象

table.appendChild(tr);//行加到table
var td1=document.createElement('td');//创建三个td然后加到行里
var td2=document.createElement('td');
var td3=document.createElement('td');
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
var sele1=document.createElement('select');//创建select放到td里
var str1="what"+count.toString();
sele1.setAttribute("name",str1);


var sele2=document.createElement('select');
var str2="how"+count.toString();
sele2.setAttribute("name",str2);

var inp=document.createElement('input');
var str3="content"+count.toString();
inp.setAttribute("name", str3);


td1.appendChild(sele1);
td2.appendChild(sele2);
td3.appendChild(inp);



for(var j=0;j<46;j++)//给第一个select放置46个我想要的option
{
var opt=document.createElement('option');
var strh="hide"+i.toString();
var hidden=document.getElementById(strh).value;


opt.setAttribute("value", hidden);
opt.setAttribute("text",hidden);
sele1.appendChild(opt);
}

var front="front"+count.toString();
var same="same"+count.toString();
var any="any"+cout.toString();
var opt1=document.createElement('option');//下面三个option是给第二个select的
opt1.setAttribute("value", front);
opt1.serAttribute("text","前方一致");

var opt2=document.createElement('option');
opt2.setAttribute("value", same);
opt2.serAttribute("text","完全匹配");


var opt3=document.createElement('option');
opt3.setAttribute("value", any);
opt3.serAttribute("text","任意匹配");



count=count+1;
}


</script>




-----------------------------------
我用了一个button的onclick事件触发上面的js函数,但是点击之后我要创建的那几个元素还没来得及闪一下,页面就自己刷新了,我创建的元素就没了,有哪位知道是怎么回事吗





...全文
285 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2015-10-02
  • 打赏
  • 举报
回复
很多错误啊,。变量都写的不对 i哪里定义了,cout也写错了 opt1.value=front; opt1.text="前方一致";
孟子E章 2015-10-02
  • 打赏
  • 举报
回复
很多错误啊,。变量都写的不对 i哪里定义了,cout也写错了 opt1.value=front; opt1.text="前方一致";
lioshey 2015-10-02
  • 打赏
  • 举报
回复
我上面的最后少了几个appendChild,后来加上去了还是没能生成option...
lioshey 2015-10-02
  • 打赏
  • 举报
回复
引用 2 楼 jslang 的回复:
button要用普通按钮,不要用提交按钮或图片按钮
好像出了其他的问题。。。我的option生成不出来,点开之后的select是没有option的,但是我看着应该这样语法没错啊
天际的海浪 2015-10-02
  • 打赏
  • 举报
回复
button要用普通按钮,不要用提交按钮或图片按钮
lioshey 2015-10-02
  • 打赏
  • 举报
回复
找到原因了.....原来我把button放在了一个form里面,然后点击之后就自己刷新了,移出外面就好了

87,984

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧