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

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

本文关键字:填充 有一个 颜色 如果 输入 文本 是否 检查      更新时间:2023-09-26

我正在做一个表格,将产品的数量发送到一个电子邮件地址(列表由php生成),用户输入所需的数量,并将其发送到一封电子邮件,我的表格需要打开颜色框窗口,只有在至少填写了一个字段的情况下,如果所有字段都为空,他点击发送,我显示警报,我尝试过了,但出现了问题。请检查小提琴;

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

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:

$(".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");
        }
    });
});

您可以首先通过以下查询找到数量>0的产品计数

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

如果长度大于0,则进行正常处理,否则显示警报

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

我认为你应该试试

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

或者

if(parseInt($(this).val()))代替if($(this).val() > 0)

如果您检查typeof $(this).val(),它将返回字符串,然后在If循环中将其与0进行比较。也许这就是问题所在。

相关文章: