2014-12-19 185 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>