简体   繁体   English

检查所有输入文本是否为空,如果至少填充了一个,则打开颜色框

[英]check if all inputs text are empty or not, and open colorbox if at least one are filled

i am doing a form to send the quantity of products to an email address (the list is generated by php), the user put the quantity desired, and send it to an email, my form need open the colorbox window, only if at least one field is filled, if all are empty, and he click to send, i show the alert, i tryed it but, something is wrong. 我正在做一个表格,将产品的数量发送到电子邮件地址(该列表由php生成),用户输入了所需的数量,然后将其发送到电子邮件,我的表格需要打开颜色框窗口,至少如果所有字段都为空,则填写一个字段,然后单击发送,我显示警报,我尝试了,但是出了点问题。 please check the fiddle; 请检查小提琴;

http://jsfiddle.net/traoLe14/2/ http://jsfiddle.net/traoLe14/2/

HTML: HTML:

 <ul>
        <li class="col-xs-2">
            <input type="number" name="1" min="0" max="100" class="form-control product" value="1">Apple</li>
        <li class="col-xs-2">
            <input type="number" name="2" min="0" max="100" class="form-control product" value="0">Banana</li>
        <li class="col-xs-2">
            <input type="number" name="3" min="0" max="100" class="form-control product" value="0">Grape</li>
        <li class="col-xs-2">
            <input type="number" name="4" min="0" max="100" class="form-control product" value="0">Potato</li>
    </ul>
    <div> <a href="#" class="btn btn-default send_products">
            <i class="glyphicon glyphicon-envelope"></i> send list 
        </a>

    </div>

JS: JS:

$(".send_products").on("click", function (e) {
    e.preventDefault();
    $(".product").each(function () {
        // print values on console
        console.log($(this).val());

        if ($(this).val() > 0) {
            $("send_products").addClass("inline", function () {
                $(".inline").colorbox({
                    inline: true,
                    height: "50%",
                    width: "50%",
                    innerWidth: 800,
                    innerHeight: "50%"
                });
            });
        } else {
            alert("no products added");
        }
    });
});

you can first find the count of products with quantity > 0 with the below query 您可以使用以下查询首先找到数量> 0的产品数

var length = $(".product").filter(function () {
        return $(this).val() > 0;
    }).length;

if length is > 0, do normal processing, else show alert 如果长度> 0,则进行正常处理,否则显示警报

http://jsfiddle.net/mfarouk/traoLe14/3/ http://jsfiddle.net/mfarouk/traoLe14/3/

I think you should try 我认为你应该尝试

if($(this).val().length > 0) instead of if($(this).val() > 0) if($(this).val().length > 0)而不是if($(this).val() > 0)

or maybe 或者可能

if(parseInt($(this).val())) instead of if($(this).val() > 0) if(parseInt($(this).val()))而不是if($(this).val() > 0)

If you check the typeof $(this).val() it will return string and then in if loop you are comparing it with 0. Maybe that's the issue. 如果检查typeof $(this).val() ,它将返回字符串,然后在if循环中将其与0进行比较。也许就是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM