【问题标题】:X-Editable - How to pull form data using ajaxX-Editable - 如何使用 ajax 提取表单数据
【发布时间】:2015-04-12 20:23:44
【问题描述】:

我正在使用 x-editable,想知道如何使用 jquery 和 ajax 填充我的选择元素。

[编辑 - 为清楚起见]

这是我的代码:

jQuery(document).ready(function() {

    //toggle `popup` / `inline` mode
    $.fn.editable.defaults.mode = 'popup';

    var getSource = function() {
        var url = "/api/rpc/payments/status_options";
        $.ajax({
            type:  'GET',
            async: true,
            url:   url,
            dataType: "json",

            success: function(responseObject){
            }

        });

    };

    //make status editable
    $('.payments-click').editable({
        type: 'select',
        title: 'Select status',
        placement: 'right',
        value: 2,
        source: getSource()
        /*
         //uncomment these lines to send data on server
         ,pk: 1
         ,url: '/post'
         */
    });

});

我正在尝试获取来源:

source: getSource()

来自函数,但不是 100% 确定如何从 Ajax 调用返回数据。

【问题讨论】:

    标签: javascript jquery ajax twitter-bootstrap x-editable


    【解决方案1】:

    我在这篇文章的帮助下解决了这个问题:How do I return the response from an asynchronous call?

    这是我的解决方案:

    jQuery(document).ready(function() {
    
        //toggle `popup` / `inline` mode
        $.fn.editable.defaults.mode = 'popup';
    
        function getSource() {
            var url = "/api/rpc/payments/status_options";
            return $.ajax({
                type:  'GET',
                async: true,
                url:   url,
                dataType: "json"
            });
        }
    
        getSource().done(function(result) {
    
            $('.payments-click').editable({
                type: 'select',
                title: 'Select status',
                placement: 'right',
                value: 2,
                source: result
                /*
                 //uncomment these lines to send data on server
                 ,pk: 1
                 ,url: '/post'
                 */
            });
    
    
        }).fail(function() {
            alert("Error with payment status function!")
        });
    
    });
    

    【讨论】:

      【解决方案2】:

      试试这个

      <a href="javascript:void(0)" id="status" data-type="select" data-title="Select Status"></a>
      <script>
          $.getJSON("api/rpc/payments/status_options", function (list) {
                  $('#status).editable({
                          prepend: "",
                          pk: 1,
                          source: list,
                          name: "status"
                 });
           });
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多