【问题标题】:X-Editable JQuery Plugin - Select Get Source ObjectX-Editable JQuery 插件 - 选择获取源对象
【发布时间】: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 可以做这个吗?

标签: javascript jquery x-editable


【解决方案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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2012-12-27
    • 2012-05-18
    • 1970-01-01
    • 2013-04-22
    相关资源
    最近更新 更多