日期:2014-05-20  浏览次数:20499 次

给 cpp2017(慕白兄)另开一 100 分贴:总是已经解决一大半了!补分!关于 javascript 校验的问题!


原贴:

http://community.csdn.net/Expert/topic/5328/5328600.xml?temp=.3653681

老大的这个回复非常有技术性,已经实现了无刷新添加项目到   listbox   :

<DIV   id= "div1 ">
<INPUT   ID= "File1 "   TYPE= "file "   NAME= "File1 "   RUNAT= "server "> <INPUT   TYPE= "button "   VALUE= "Button "   onclick= "javascript:AddFile(); ">
<ASP:LISTBOX   id= "ListBox1 "   runat= "server "> </ASP:LISTBOX>
<ASP:BUTTON   id= "Button1 "   runat= "server "   Text= "Button "> </ASP:BUTTON> </FONT>
</DIV>


<SCRIPT   language= "javascript ">
<!--
  function   AddFile()
  {
var   file   =   document.getElementById( "div1 ").firstChild;
if(file.value   ==   " ")
{
alert( "请选择文件! ");
return;
}
var   o   =   new   Option();
var   ary   =   file.value.split( "\\ ");
var   filename   =   ary[ary.length-1];
o.innerText   =   filename;
o.value   =   filename;
document.getElementById( "ListBox1 ").appendChild(o);

file.style.display   =   "none ";
var   f   =   document.createElement( "input ");
f.type   =   "file ";
f.name   =   "file "
div1.insertBefore(f,div1.firstChild);

 

  }
//-->
</SCRIPT>

现在有一个问题,就是我想实现一个   点中   listbox   中的一个项后,再点击一个按钮能实现删除这个   listbox   选中项的功能,就是再写一个与   function   AddFile()
  对应的   function   RemoveFile()   的   javascript   函数。


这个问题如果解决了,就完美了!!




------解决方案--------------------
<SCRIPT language= "javascript "> <!-- function AddFile() { var file = document.getElementById( "div1 ").firstChild; if(file.value == " ") { alert( "请选择文件! "); return; } var o = new Option(); var ary = file.value.split( "\\ "); var filename = ary[ary.length-1]; if(CheckOptionsExists(filename,document.getElementById( "ListBox1 "))) { alert( "文件已经存在列表中! "); return; } var f = document.createElement( "input "); f.type = "file "; f.name = "file " div1.insertBefore(f,div1.firstChild); o.innerText = filename; o.value = f.uniqueID; document.getElementById( "ListBox1 ").appendChild(o); file.style.display = "none "; } function RemoveFile() { var lst = document.getElementById( "ListBox1 "); if(lst.selectedIndex == -1) { alert( "请选择要删除的附件! "); return; } var id = lst.value; div1.removeChild(document.all[id]); lst.removeChild(lst.options[lst.selectedIndex]); } //检查选项是否存在. function CheckOptionsExists(value,ddl) { for(var i=0;i <ddl.options.length;i++) { if(ddl.options[i].innerText == value) { return true; } } return false; } //--> </SCRIPT>
------解决方案--------------------
<DIV id= "div1 "> <INPUT ID= "File1 " TYPE= "file " NAME= "File1 " runat= "server "> <INPUT TYPE= "button " VALUE= "添加附件 " onclick= "javascript:AddFile(); "> <INPUT TYPE= "button " VALUE= "删除附件 " onclick= "javascript:RemoveFile(); "> <ASP:LISTBOX id= "ListBox1 " Width= "200px " Height= "100px " runat= "server "> </ASP:LISTBOX> <ASP:BUTTON id= "Button1 " runat= "server " Text= "保存 " Width= "60px "> </ASP:BUTTON> </DIV>