2017-07-19 81 views
9

我有问题试图获取选定的对象,而不是传递给成功回调的“newValue”。X-Editable JQuery插件 - 选择获取源对象

下面是一个例子:

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

感谢 卡尔

+0

你有没有看到console.log(响应)? – MVG1984

+0

是的,我已经尝试过,这是来自Ajax请求(我没有使用)的响应 –

+0

@CarlSmith可以为此做一个小提琴吗? – ProllyGeek

回答

5

可以使用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/