2017-04-26 83 views
-1

也许我所表达的问题的标题不是很清楚。让我说更具体一点。我有一些HTML代码如下:jquery如何判断from中的数组是否为空

<html> 

<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
    <script src="js/jquery.js"></script> 
</head> 

<body> 
    <form action="" method="post" enctype="multipart/form-data" onsubmit="return checkpost();"> 
     <p> <input name="picture[]" type="file" accept="image/gif, image/jpeg" /> 
     </p> 
     <p> <input name="picture[]" type="file" accept="image/gif, image/jpeg" /> 
     </p> 
     <p> <input name="picture[]" type="file" accept="image/gif, image/jpeg" /> 
     </p> 
     <p> <input id="submit" type="submit" value="submit" /></p> 
    </form> 
</body> 

<script> 
    function checkpost() { 

    } 
</script> 

</html> 

对于我的PHP编程,我要检查,如果有选择的3张图片,但我不知道如何检查,在我的js代码。 对不起,我可怜的英语使问题不太清楚,但感谢您的帮助!

+0

是否必须是明确的jQuery,或将普通的JavaScript是VAID答案吗?另外,你到目前为止尝试过什么? – gabe3886

回答

0

试试这个使用jQuery,

$('#submit').on('click', function() { 
 
    pictures = $('[name="picture[]"]'); 
 
    // filter the pictures element which has some values 
 
    filled = pictures.filter(function() { 
 
    return this.value.length 
 
    }); 
 
    // if all images are added then return true 
 
    if (filled.length === pictures.length) { 
 
    return true; 
 
    } 
 
    alert('Please select all images'); 
 
    return false; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form action="" method="post" enctype="multipart/form-data"> 
 
    <p> <input name="picture[]" type="file" accept="image/gif, image/jpeg" /> 
 
    </p> 
 
    <p> <input name="picture[]" type="file" accept="image/gif, image/jpeg" /> 
 
    </p> 
 
    <p> <input name="picture[]" type="file" accept="image/gif, image/jpeg" /> 
 
    </p> 
 
    <p> <input id="submit" type="submit" value="submit" /></p> 
 
</form>

0
You could disable the submit input until an array of values no longer contains a null; 

<html> 

<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
    <script src="js/jquery"></script> 
</head> 

<body> 
    <form action="" method="post" enctype="multipart/form-data" onsubmit="return checkpost();"> 
     <p> <input name="picture[]" type="file" accept="image/gif, image/jpeg" onchange="changeHandler" /> 
     </p> 
     <p> <input name="picture[]" type="file" accept="image/gif, image/jpeg" /> 
     </p> 
     <p> <input name="picture[]" type="file" accept="image/gif, image/jpeg" /> 
     </p> 
     <p> <input id="submit" type="submit" value="submit" disabled /></p> 
    </form> 
</body> 

<script> 
    var inputs = $("input[type=file]"), 
     submit = $("input[type=submit"); 
    inputs.on("change", changeHandler); 

    function checkpost() { 
     // carry out action 
    } 

    function changeHandler() { 
     var a = []; 

     for(var i = 0; i < inputs.length; i++) { 
      a.push($(inputs[i]).val()); 
     } 

     a.indexOf("") === -1 ? submit.prop("disabled", false) : submit.prop("disabled", true); 
    } 
</script> 

</html> 
相关问题