簡體   English   中英

檢查所有輸入文本是否為空,如果至少填充了一個,則打開顏色框

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

我正在做一個表格,將產品的數量發送到電子郵件地址(該列表由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進行比較。也許就是問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM