<!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" />
<script type="text/javascript">
window.onload = function() {

function insertRow(){
alert("ok");
var rowIndex1 = document.all("addordel").length;
if(rowIndex1 >= 5){
alert('一次最多能上传5个文件');
return false;
}
var obj=document.getElementsByName("addordel")[0].cloneNode(true);
document.all("change").appendChild(obj);

}
function delRow(){
var rowIndex = document.all("addordel").length;
if(rowIndex >= 2){
event.srcElement.parentNode.removeNode(true);
}
}
function chkfile(){
alert("okok");
obj = document.getElementsByName('upname[]');
num = obj.length;
for(i=0;i<num;i++){
if(obj[i].value == 0){
alert('请添加上传文件');
return false;
}
}
}

}

</script>
</head>
<body>
<div style=" background-image:url(images/upfile.gif); background-position:left; background-repeat:no-repeat; width: 95%; height:43px;">&nbsp;</div>
<hr style=" width: 95%; border:1px #597ecf solid;"  />
<div style=" width: 95%; height:25px; background-color: #FFFFCC; border: 1px #FFFF66; line-height: 25px; text-align:center; color:#ff0000;">&nbsp;上传文件限制:100M</div>
<form method="post" enctype="multipart/form-data" action="?act=upfile">
<ul id="change">
<li id="addordel" ><input id="upname[]" name="upname[]" type="file" style=" width: 200px; background-color:#f0f0f0; " />&nbsp;
<select id="foundtype[]" name="foundtype[]">
<?php
$typesql = "select genrename from tb_uptype";
$typearr = $conne->getRowsArray($typesql);
$conne->close_rst();
foreach($typearr as $value){
?>
<option value="<?php echo $value['genrename']; ?>"><?php echo $value['genrename']; ?></option>
<?php
}
?>
</select>&nbsp;
<select id="ispub[]" name="ispub[]">
<option value="0" selected="selected">不公开</option>
<option value="1">公开</option>
</select>&nbsp;
<button onClick="delRow()" style=" border: 1px #CCCCCC solid; background-color:#f0f0f0;">删除</button>&nbsp;<button onClick="insertRow()" style=" border: 1px #cccccc solid; background-color: #f0f0f0;">增加</button>&nbsp;</li>
</ul>
<input id="upbtn" name="upbtn" type="submit" value="上传" onClick="return chkfile()" style=" background-color:#f0f0f0; border: 1px #CCCCCC solid;" />
</form>

</body>
</html>
请问:
怎么无法触发onCick事件。

解决方案 »

  1.   


            //window.onload = function() { //去掉
            
                function insertRow(){
                    alert("ok");
                    var rowIndex1 = document.all("addordel").length;
                    if(rowIndex1 >= 5){
                        alert('一次最多能上传5个文件');
                        return false;
                    }
                    var obj=document.getElementsByName("addordel")[0].cloneNode(true);
                    document.all("change").appendChild(obj);
            
                }
                function delRow(){
                alert("fff");
                    var rowIndex = document.all("addordel").length;
                    if(rowIndex >= 2){
                        event.srcElement.parentNode.removeNode(true);
                    }
                }
                function chkfile(){
                    alert("okok");
                    obj = document.getElementsByName('upname[]');
                    num = obj.length;
                    for(i=0;i<num;i++){
                        if(obj[i].value == 0){
                            alert('请添加上传文件');
                            return false;
                        }
                    }
                }
            
            //}//去掉
            
      

  2.   

    <input id="upbtn" name="upbtn" type="submit" value="上传" onClick="return chkfile()" style=" background-color:#f0f0f0; border: 1px #CCCCCC solid;" />type='submit'的按钮点击时会提交页面,并刷新页面,后面的js自然没法执行了
    你可以改为
    把type改为button
    然后点击事件中用ajax方式提交表单
      

  3.   

    1:你要么将你的fn移出系统事件绑定,即从这里面window.onload = function() {}移出来
    2:要么,在window.onload = function() {
    //在这里直接绑定事件,比如你最后的上传事件绑定
    document.getElementById("upbtn").onclick=chkfile;
    }
      

  4.   

    呵呵,未知数先生,走眼了撒。他那样子定义,啥啥click都木有用滴哇
      

  5.   

    看清楚点 人家有return false的 如果是false是不会submit的