var navRoot = document.getElementById("orderform"); for (i=0; i<navRoot.childNodes.length; i++){ node = navRoot.childNodes[i]; if (node.nodeName=="input"){ // }
var navRoot = document.getElementById("orderform"); for (i=0; i<navRoot.childNodes.length; i++){ node = navRoot.childNodes[i]; if (node.nodeName=="INPUT"){ // }
我刚用if(document.getElementById) { 支持脚本 } 测试了,可以运行呀。但为什么我那条运行的时候不正常?我的代码是判断所有text是否都为空 我的代码是:function foo(e) { var input_cart=document.getElementById("orderform").getElementsByTagName("input"); var input_all=0; for (var i=0; i<input_cart.length; i++) { if (input_cart[i].type=="text" && input_cart[i].value!="") {
input_all=input_all+1; //alert(input_all); } } if (input_all==0) { alert("Please input one of the quantity of products that you want before you submit,thanks!"); return false; } }不管我在text里面输入什么值都返回是input_all==0
当然也可以直接 var input_cart=document.forms["orderform"].getElementsByTagName("input"); 这样是FF和IE都通用的
这样 <html> <script type="text/javascript" language="javascript"> function foo() { var input_cart=document.getElementById("orderform").getElementsByTagName("input"); var input_all=0; for (var i=0; i <input_cart.length; i++) { if(input_cart[i].type=="text" && input_cart[i].value!="") { input_all=input_all+1; alert(input_all); } } if (input_all==0) { alert("Please input one of the quantity of products that you want before you submit,thanks!"); return false; } } </script> <body> <form name="orderform" id="orderform"> <input id="tete" type="text" value="test" /> <input type="button" value="test" onclick="foo()" /> </form> </body> </html>
var navRoot = document.getElementById("orderform");
for (i=0; i<navRoot.childNodes.length; i++){
node = navRoot.childNodes[i];
if (node.nodeName=="input"){
//
}
var navRoot = document.getElementById("orderform");
for (i=0; i<navRoot.childNodes.length; i++){
node = navRoot.childNodes[i];
if (node.nodeName=="INPUT"){
//
}
那如何进一步判断type为txt及value值呢?
obj.getAttribute("value") && obj.value
if(document.getElementById)
{
支持脚本
}
{
支持脚本
}
测试了,可以运行呀。但为什么我那条运行的时候不正常?我的代码是判断所有text是否都为空
我的代码是:function foo(e) {
var input_cart=document.getElementById("orderform").getElementsByTagName("input");
var input_all=0;
for (var i=0; i<input_cart.length; i++) {
if (input_cart[i].type=="text" && input_cart[i].value!="") {
input_all=input_all+1;
//alert(input_all);
}
}
if (input_all==0)
{
alert("Please input one of the quantity of products that you want before you submit,thanks!");
return false;
}
}不管我在text里面输入什么值都返回是input_all==0
[code=JScri pt][\/code]
这种的标记贴出来...
<script>
var myform = document.getElementById("orderform");
var input_cart=myform.getElementsByTagName("input");
</script>
<form name="orderform"></form>
IE下表单元素的name属性可以当ID用
但FF严格要求要ID属性所以FF下会出错
如果你这么写
<form name="orderform" id="orderform"></form>
那么FF下面就没问题了
var input_cart=document.forms["orderform"].getElementsByTagName("input");
这样是FF和IE都通用的
<html>
<script type="text/javascript" language="javascript">
function foo()
{
var input_cart=document.getElementById("orderform").getElementsByTagName("input");
var input_all=0;
for (var i=0; i <input_cart.length; i++)
{
if(input_cart[i].type=="text" && input_cart[i].value!="")
{
input_all=input_all+1;
alert(input_all);
}
}
if (input_all==0)
{
alert("Please input one of the quantity of products that you want before you submit,thanks!");
return false;
}
}
</script>
<body>
<form name="orderform" id="orderform">
<input id="tete" type="text" value="test" />
<input type="button" value="test" onclick="foo()" />
</form>
</body>
</html>
else{
支持脚本
}
在IE中代码换行不会被认为是一个元素
而FF会认为这是一个元素 只不过他会认为是一个没东西的text元素这点很有趣 呵呵