问题描述:

<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>

With above script I'd like to verify if "name" and "city" form inputs are not empty. Somehow I can't get this working, condition is still returning false even if inputs are filled with text. Here are inputs:

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

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

I was also trying:

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

None of the above methods worked for me. Whats wrong here?

网友答案:

you have an error in your code, there is one closing bracket too much

change

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

to

 if (document.form['form']['name'].value != "" && 
    document.form['form']['city'].value != "" ) {
网友答案:
if(  document.getElementById("ValidateUsename").innerHTML =="")
{
alert("box is empty")
}
网友答案:

There are several things that might be wrong here

firstly you have a javascript issue on you 'if' statement (too many brackets)

you are using ['form'] but there is no form element shown. you have document.getElementById('submit') but there is no submit element shown and you have document.getElementById('uploading') but there is no uploading element.

Without knowing if they exist there is no way of knowing what the problem is. you also say that 'None of the above methods worked for me' but what IS happening? is anything happening, surley you have console errors?

网友答案:

try like this: you have syntax error in if condition.

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>
相关阅读:
Top