X-Editable JQuery 插件 - 选择获取源对象

Posted

技术标签:

【中文标题】X-Editable JQuery 插件 - 选择获取源对象【英文标题】:X-Editable JQuery Plugin - Select Get Source Object 【发布时间】:2017-12-24 13:51:28 【问题描述】:

我在尝试获取所选对象而不是传递给成功回调的“newValue”时遇到问题。

这是一个例子:

$("select").editable(
        type : "select",
        title: 'Select Fruit',
        source : [
            text : "Apple", value: "option_1",
            text : "Orange", value: "option_2",
            text : "Mango",value: "option_3",
            text : "Strawberry",value: "option_4"
        ],
        success:function(response,newValue)
            console.log(newValue); // newValue contains the "text" string ok..
            // How do I get the selected source object? eg. text : "Orange", value: "option_2"
            // So I can access the object like..
            console.log(newValue.value); // output option_*
        
    );

谢谢 卡尔

【问题讨论】:

你看到console.log(response)了吗? 是的,我试过了,那是来自 Ajax 请求的响应(我没有使用) @CarlSmith 可以做这个吗? 【参考方案1】:

您可以使用display 回调来访问value,甚至可以访问整个选定对象:

<a href="#" id="status" data-type="select" data-pk="1" data-title="Select status"></a>    
<script>
  $(function() 

    $("#status").editable(
      type: "select",
      title: 'Select Fruit',
      source: [
        text : "Apple", value: "option_1",
        text : "Orange", value: "option_2",
        text : "Mango",value: "option_3",
        text : "Strawberry",value: "option_4"
      ],
      display: function(value, sourceData) 

        if (value)  // value = "option_3" etc.
          $(this).html(value);
        

        /* OR if you want to access the selected source object  ...

        var selected = $.fn.editableutils.itemsByValue(value, sourceData); 
        if (selected.length) 
           $(this).html(selected[0].value);
          */
      
    );
  );
</script>

演示:http://jsfiddle.net/6vzrug72/

【讨论】:

以上是关于X-Editable JQuery 插件 - 选择获取源对象的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 x-editable 和 jquery 验证插件

php 使用jquery的x-editable插件进行Laravel 5 inplace编辑。内联和批量编辑示例。

x-editable插件怎么用

一些常用的jQuery插件

动态添加元素上的 X-Editable 引导插件“隐藏”事件问题

使用x-editable更新行中的值后jquery数据表未排序