2014-12-19 186 views
-4
<script> 
function showUploading() { 

    if (document.form['form']['name'].value != "" && 
     document.form['form']['city'].value != "")) { 

     document.getElementById('submit') .style.visibility = "hidden"; 
     document.getElementById('uploading').style.visibility = "visible"; 
    } 
}      
</script> 

使用上面的脚本我想验证“name”和“city”表单输入是否为空。不知何故,我无法得到这个工作,即使输入是充满文本条件仍然返回false。下面是输入:Javascript - 验证空输入

<input required="required" id="name" name="name" type="text"> 
<input required="required" id="city" name="city" type="text"> 

我也想:

if (document.getElementById('name').value != "") 

上述方法都没有为我工作。这里有什么问题?

+2

其中是将触发showUploading代码()? – atmd 2014-12-19 10:13:18

+1

_这里有什么错?_在这里,什么都没有。也许在其他地方。 – melancia 2014-12-19 10:14:45

回答

0

你有一个错误在你的代码中,有一个右括号太多

变化

if (document.form['form']['name'].value != "" && 
    document.form['form']['city'].value != "")) { 

if (document.form['form']['name'].value != "" && 
    document.form['form']['city'].value != "") { 
+0

可能这是一个问题,并且不适用于document.form,而是适用于document.forms。 – 2014-12-19 11:10:36

0
if( document.getElementById("ValidateUsename").innerHTML =="") 
{ 
alert("box is empty") 
} 
+0

输入字段为什么你会使用内部的HTML你应该使用.value。 – 2014-12-19 10:18:35

0

有几件事情,可能是错的这里

首先,你有一个JavaScript问题'如果'语句(太多括号)

你正在使用['form']但没有显示表单元素。 你有document.getElementById('submit')但没有显示提交元素,你有document.getElementById('uploading')但没有上传元素。

不知道它们是否存在,无法知道问题是什么。 你也会说'上述方法都不适用于我',但是发生了什么?有什么事情发生,你有控制台错误吗?

0

尝试像这样:if语句中有语法错误。

function showUploading() { 
 

 
    if (document.getElementById('name').value.trim() != "") {// trim to avoid initial spaces 
 
      alert(document.getElementById('name').value); 
 
     //document.getElementById('submit') .style.visibility = "hidden"; 
 
     //document.getElementById('uploading').style.visibility = "visible"; 
 
     
 
     //rest of your code 
 
    } 
 
}
<input required="required" id="name" name="name" type="text"> 
 
<input required="required" id="city" name="city" type="text"> 
 

 
<button onclick="showUploading()">click</button>