请问:如何动态创建一个ListBox控件

leetow2006 2018-07-06 04:59:39
我在html中有一个:
《input name="ucust" type="text" id="ucust" onkeyup="getv()"/》
我想在点击getv()时能动态创建一个和上面的input的宽度是一样的ListBox控件,
请问如何写代码?另外:如何动态销毁这个动态控件?
...全文
410 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ambit_tsai-微信 2018-07-08
  • 打赏
  • 举报
回复
引用 7 楼 leetow2006 的回复:
请问:我在按按钮时,想先执行一段JS代码,然后再跳转到其他文件,比如:
《form id="form1" name="form1" method="post" action="/fjqsl/ord/sltordsale.php"》
想问在action中,能否先执行一段本地文件中的JS代码,然后再跳转到/fjqsl/ord/sltordsale.php?
这个该怎么做呢?


<script>
// 监听表单提交的onsubmit事件
document.querySelector('#form1').onsubmit = function () {
alert(123);
return false; // 返回false可以阻止提交
}
</script>
leetow2006 2018-07-08
  • 打赏
  • 举报
回复
请问:我在按按钮时,想先执行一段JS代码,然后再跳转到其他文件,比如:
《form id="form1" name="form1" method="post" action="/fjqsl/ord/sltordsale.php"》
想问在action中,能否先执行一段本地文件中的JS代码,然后再跳转到/fjqsl/ord/sltordsale.php?
这个该怎么做呢?
ambit_tsai-微信 2018-07-08
  • 打赏
  • 举报
回复
script标签要写在form标签后面!!!
leetow2006 2018-07-08
  • 打赏
  • 举报
回复
引用 8 楼 ambit_tsai 的回复:
[quote=引用 7 楼 leetow2006 的回复:]
请问:我在按按钮时,想先执行一段JS代码,然后再跳转到其他文件,比如:
《form id="form1" name="form1" method="post" action="/fjqsl/ord/sltordsale.php"》
想问在action中,能否先执行一段本地文件中的JS代码,然后再跳转到/fjqsl/ord/sltordsale.php?
这个该怎么做呢?


<script>
// 监听表单提交的onsubmit事件
document.querySelector('#form1').onsubmit = function () {
alert(123);
return false; // 返回false可以阻止提交
}
</script>
[/quote]
我这样写:
<script>
// 监听表单提交的onsubmit事件
document.querySelector('#form1').onsubmit = function () {
alert(123);
return false; // 返回false可以阻止提交
}
</script>

<body>
<form id="form1" name="form1" method="post" action="/fjqsl/ord/sltordsale.php" >
.....
</body>
可是没有反应,没有弹出123,也没有阻止/fjqsl/ord/sltordsale.php程序的提交。
请问我错在哪了?
leetow2006 2018-07-07
  • 打赏
  • 举报
回复
引用 2 楼 net_lover 的回复:
ListBox控件在html中就是select标签。动态创建的方法参见代码,至于宽度设置样式即可实现
https://blog.csdn.net/qq_21703215/article/details/58599911

有几个问题想请教下:
1、我现在能创建select控件,但是每一次按下都会创建一次,有什么办法能判断如果已经创建,就不要再创建了?
2、我想创建的select控件在:
《input name="ucust" type="text" id="ucust" onkeyup="getv()"/》
的正下方,我这样做不行:
var pcust = document.getElementById("ucust");
var returnbox = document.createElement("select");
returnbox.id = "mySelect";
document.body.appendChild(pcust);
3、这个控件中的选项option,能响应点击事件,请问怎么写?
孟子E章 2018-07-07
  • 打赏
  • 举报
回复
ListBox控件在html中就是select标签。动态创建的方法参见代码,至于宽度设置样式即可实现
https://blog.csdn.net/qq_21703215/article/details/58599911

leetow2006 2018-07-07
  • 打赏
  • 举报
回复
怎么没人回答?自己顶
luckczj 2018-07-07
  • 打赏
  • 举报
回复

luckczj 2018-07-07
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
function getv(){
var txt=document.getElementById("ucust");
var kd=txt.offsetWidth;
var div=document.getElementById("cc");
var lst="<select name='lst' size='5' id='lst' style='width:"+kd+"px;'>";
lst+="<option value='1'>v1</option>";
lst+="<option value='2'>v2</option>";
lst+="<option value='3'>v3</option></select>";
if(div.innerHTML!="")
{
div.innerHTML="";
}
else
{
div.innerHTML=lst;
}
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<p>
<input type="text" name="ucust" id="ucust" onkeyup="getv()"/>
</p>
<p>
<label for="lst"></label>

</p>
<div id="cc"></div>
</form>
</body>
</html>
ambit_tsai-微信 2018-07-07
  • 打赏
  • 举报
回复
引用 3 楼 leetow2006 的回复:
有几个问题想请教下:
1、我现在能创建select控件,但是每一次按下都会创建一次,有什么办法能判断如果已经创建,就不要再创建了?
2、我想创建的select控件在:
《input name="ucust" type="text" id="ucust" onkeyup="getv()"/》
的正下方,我这样做不行:
var pcust = document.getElementById("ucust");
var returnbox = document.createElement("select");
returnbox.id = "mySelect";
document.body.appendChild(pcust);
3、这个控件中的选项option,能响应点击事件,请问怎么写?


document.body.innerHTML+='<input name="ucust" type="text" id="ucust" onkeyup="getv(this)"/>';

function getv(el){
console.log(el.offsetWidth); // input宽度

if(!document.getElementById('select')){ // 判断是否已有select
var returnbox = document.createElement('select');
returnbox.id = 'mySelect';
returnbox.onchange = function(){ // 利用select的onchange方法来监听并响应option的选择
console.log(this.value); // 将输出123或者456,对其做判断即可
};
var option = document.createElement('option');
option.text = 123;
option.value = 123;
returnbox.appendChild(option); // 将option元素插入select中
option = document.createElement('option');
option.text = 456;
option.value = 456;
returnbox.appendChild(option);
document.body.appendChild(returnbox); // 将select元素插入body中
}
}
QQ好友例表控件 带实例和源码 //1. 属性列表: // SelectionMode 组件中条目的选择类型,即多选(Multiple)、单选(Single) // Rows 列表框中显示总共多少行 // Selected 检测条目是否被选中 // SelectedItem 返回的类型是ListItem,获得列表框中被选择的条目 // Count 列表框中条目的总数 // SelectedIndex 列表框中被选择项的索引值 // Items 泛指列表框中的所有项,每一项的类型都是ListItem //2. 取列表框中被选中的值 // ListBox.SelectedValue //3. 动态的添加列表框中的项: // ListBox.Items.Add("所要添加的项"); //4. 移出指定项: // //首先判断列表框中的项是否大于0 // If(ListBox.Items.Count > 0 ) // { ////移出选择的项 //ListBox.Items.Remove(ListBox.SelectedItem); // } //5. 清空所有项: // //首先判断列表框中的项是否大于0 // If(ListBox.Items.Count > 0 ) // { ////清空所有项 //ListBox.Items.Clear(); // } //6. 列表框可以一次选择多项: // 只需设置列表框的属性 SelectionMode="Multiple",按Ctrl可以多选 //7. 两个列表框联动,即两级联动菜单 // //判断第一个列表框中被选中的值 // switch(ListBox1.SelectValue) // { ////如果是"A",第二个列表框中就添加这些: //case "A" // ListBox2.Items.Clear(); // ListBox2.Items.Add("A1"); // ListBox2.Items.Add("A2"); // ListBox2.Items.Add("A3"); ////如果是"B",第二个列表框中就添加这些: //case "B" // ListBox2.Items.Clear(); // ListBox2.Items.Add("B1"); // ListBox2.Items.Add("B2"); // ListBox2.Items.Add("B3"); // } //8. 实现列表框中项的移位 // 即:向上移位、向下移位 // 具体的思路为:创建一个ListBox对象,并把要移位的项先暂放在这个对象中。 // 如果是向上移位,就是把当前选定项的的上一项的值赋给当前选定的项,然后 // 把刚才新加入的对象的值,再附给当前选定项的前一项。 // 具体代码为: // //定义一个变量,作移位用 // index = -1; // //将当前条目的文本以及值都保存到一个临时变量里面 // ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue); // //被选中的项的值等于上一条或下一条的值 // ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text; // //被选中的项的值等于上一条或下一条的值 // ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value; // //把被选中项的前一条或下一条的值用临时变量中的取代 // ListBox.Items[ListBox.SelectedIndex].Test=lt.Test; // //把被选中项的前一条或下一条的值用临时变量中的取代 // ListBox.Items[ListBox.SelectedIndex].Value=lt.Value; // //把鼠标指针放到移动后的那项上 // ListBox.Items[ListBox.SelectedIndex].Value=lt.Value; //9. 移动指针到指定位置: // (1).移至首条 // //将被选中项的索引设置为0就OK了 // ListBox.SelectIndex=0; // (2).移至尾条 // //将被选中项的索引设置为ListBox.Items.Count-1就OK了 // ListBox.SelectIndex=ListBox.Items.Count-1; // (3).上一条 // //用当前被选中的索引去减 1 // ListBox.SelectIndex=ListBox.SelectIndex - 1; // (4).下一条 // //用当前被选中的索引去加 1 // ListBox.SelectIndex=ListBox.SelectIndex + 1; //this.ListBox1.Items.Insertat(3,new ListItem("插入在第3行之后项","")); //this.ListBox1.Items.Insertat(index,ListItem) //ListBox1.Items.Insert(0,new ListItem("text","value"));

87,909

社区成员

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

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