首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JS插入数据,该如何处理

2012-06-02 
JS插入数据这条语句就是判断 id为l的值是否为空,如果为空,则返回,都不为空,则提交...下面语句有点BUG,就是

JS插入数据
这条语句就是判断 id为l的值是否为空,如果为空,则返回,都不为空,则提交...下面语句有点BUG,就是不管为不为空,都会跳转 到 pi.asp,所以麻烦大家帮我帮下面语句改下


<script language="javascript">
function pd()
{
var a=document.getElementById("l").value
for (i=0;i<a;i++)
 {
if(document.getElementById("d"+i).value=="")
  {
alert("数量不能为空")
break
  }
 }
this.form1.submit()
window.location.href="PI.ASP"

}



</script>

[解决办法]

JScript code
function pd() {    var a = document.getElementById("l").value; //先确保这个id在你的页面存在啊    for (i = 0; i < a; i++) {        if (document.getElementById("d" + i).value == "") {            alert("数量不能为空")            //break            return; //要return 才阻止程序继续往下走        }    }    //this.form1.submit()    document.form1.action = "PI.ASP"; //直接设定form的action 提交到asp页面    document.form1.submit(); //提交form    //window.location.href = "PI.ASP"}
[解决办法]
HTML code
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title></head><body style="width:120px;"><form action="PI.ASP" method="post" name="form1">  <table><tr><td><input type="text" id="l1"></td><td><input type="text" id="l2"></td></tr></table><input type="button" onclick="pd()" value="click me"></form><script type="text/javascript">function pd(){    var inps = document.getElementsByTagName('input'),    len = inps.length,is_true = true;        for(var i = 0; i < len; i++){        if(inps[i].value == '' && inps[i].type == 'text'){            is_true = false;        }    }        if(is_true){        document.form1.submit();        }else{        alert('文本框不能为空')        }};</script></body></html> 

热点排行
Bad Request.