x-editable-自动启动多个选择元素

x-editable - automatically initiate multiple select elements

本文关键字:选择 元素 自动启动 x-editable-      更新时间:2023-09-26

我的页面包含多个数据类型为"select"的x可编辑元素:

<a href="#" data-type="select" data-name="services_0" data-url="/customer/editfield" data-pk="1028" id="services_0">True</a>
<a href="#" data-type="select" data-name="services_1" data-url="/customer/editfield" data-pk="1028" id="services_1">False</a>

我正试图用以下javascript代码用x-editable启动它们:

var services = ['services_0', 'services_1'];
    $.each(services, function(index, value) {
        $('#' + value).editable({
            prepend: 'Select one',
            source: [
            {
                value: 1,
                text: 'True'
            },
            {
                value: 0,
                text: 'False'
            }
            ],
            display: function(value, sourceData) {
                var colors = {
                    1: "green",
                    0: "blue"
                },
                elem = $.grep(sourceData, function(o) {
                    return o.value == value;
                });
                if (elem.length) {
                    $(this).text(elem[0].text).css("color", colors[value]);
                } else {
                    $(this).empty();
                }
            }
        });
    });

问题是它只绑定第一个元素。

检查页面源时,我发现查询$.grep在e.length()上给了我一个TypeError。它触发了此行的"value":

display: function(value, sourceData) {

一旦我将data value="添加到元素中,一切都按预期进行:

<a href="#" data-value="" data-type="select" data-name="services_0" data-url="/customer/editfield" data-pk="1028" id="services_0">True</a>
<a href="#" data-value="" data-type="select" data-name="services_1" data-url="/customer/editfield" data-pk="1028" id="services_1">False</a>