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

时间:2022-12-09 17:14:13

I am having issues trying to get the selected object rather than the "newValue" that is passed to the success callback.

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

Here is an example:

这是一个例子:

$("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_*
        }
    });

Thanks Carl

谢谢卡尔

1 个解决方案

#1


5  

You can use the display callback to access value, or even the entire selected object:

您可以使用显示回调来访问值,甚至是整个所选对象:

<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>

Demo: http://jsfiddle.net/6vzrug72/

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

#1


5  

You can use the display callback to access value, or even the entire selected object:

您可以使用显示回调来访问值,甚至是整个所选对象:

<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>

Demo: http://jsfiddle.net/6vzrug72/

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