来自Excel问题的Mac剪贴板数据

时间:2016-11-21 14:24:44

标签: javascript html macos



$(function() {
    quickOrder();
});

function quickOrder() {

    $("#quick-order input[type='text']").val("");

    var order = $(".quick-order");
    if (!order.length) {
        return;
    }

    var items = order.find(".items");
    var tpl = items.prev(".item-template").html();
    var button = order.find("button");

    order.find(".show-more").unbind().on("click", function(e) {
        e.preventDefault();
        add(3);
        typeahead();
    });

    order.on("paste", "input", function(e) {
        var paste;
        var rows;

        if (window.clipboardData && window.clipboardData.getData) { // IE
            paste = window.clipboardData.getData('Text');
            rows = paste.split("\n");
            alert("Ja")
        } else if (e.originalEvent.clipboardData && e.originalEvent.clipboardData.getData) { // other browsers
            paste = e.originalEvent.clipboardData.getData('text/plain');
            rows = paste.split("\n");
            console.log(rows)
        }

        for (var i in rows) {
            rows[i] = rows[i].split("\t");
            if (!rows[i][1]) {
                rows[i][1] = "";
            }
        }

        var current = $(this);
        var qty = current.next("input");

        var index = current.parents(".item").attr("id").replace("item-", "");

        if (!isNaN(index)) {
            index = parseInt(index);
        } else {
            index = 0;
        }

        var count = items.find(".item").length;
        var overflow = count - rows.length - index;

        if (overflow < 0) {
            add(Math.abs(overflow));
        }

        for (var i in rows) {
            var row = rows[i];
            var num = row[0];
            var qty = row[1];

            var item = items.find("#item-" + (parseInt(i) + index));

            (function() {
                var n = item.find(".num");
                var q = item.find(".qty");

                var nn = num;
                var qq = qty;

                setTimeout(function() {
                    n.val(nn);
                    q.val(qq);
                }, 0);
            })();
        }

        order.find("input[type=text]").unbind("focus blur");
    });

    function add(num) {
        var html = new Array(num + 1).join(tpl);
        items.append(html);

        var id = 0;
        items.find(".item").each(function() {
            var item = $(this);
            item.attr("id", "item-" + id);
            id++;
        });

        button.appendTo(items.find(".item").last());
    }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
<div id="quick-order" class="quick-order">
    <div class="description">
        <i class="fa fa-info-circle">
        	<div class="tt">Test</div>
        </i>
    </div>
    <div class="item-template" style="display: none;">
        <div class="item">
            <input type="text" class="num" name="ProductCode[]" placeholder="Prod no.">
            <input type="text" class="qty" name="ProductQuantity[]" placeholder="Qty">
        </div>
    </div>
    <div class="items">
        <div class="item" id="item-@i">
            <input type="text" class="num" name="ProductCode[]" placeholder="Prod no.">
            <input type="text" class="qty" name="ProductQuantity[]" placeholder="Qty">

            <button class="quick-add-to-cart-button load-button">
        	uu
        	</button>

        </div>

    </div>
    <a href="javascript:void(0);" class="show-more">

    </a>

</div>
&#13;
&#13;
&#13;

这是我添加excel行并根据剪贴板数量添加更多字段的代码。

您可以尝试从Excel复制两列并将其粘贴。但是,这似乎不适用于Mac。它适用于所有其他浏览器的Windows。

任何人都可以帮我找出问题吗?

这是上面带有console.log(行)的Windows计算机的输出:

["1 2", "3  4", "5  6", "7  8", ""]

在mac:

["1 2 3 4 5 6 7 8"] 

1 个答案:

答案 0 :(得分:1)

将分割线更改为

....split(/\r\n|\r|\n/g)

看看它是否收回了回车。