1

我正在尝试使用 X-editable select2 来允许用户在图像下插入标签。我有它来创建标签,您可以单击并弹出框进行编辑。它还将新标签附加到页面。但是,问题是,它根本不会触发 mockjax/ajax 调用。

我已经在我的头文件中包含了 mockjax.js 文件,jquery 文件是链接的。我在浏览器控制台中没有得到任何响应。我已经在我网站的其他部分测试了 mockjax,它们都可以正确触发,只是这个似乎不起作用。

如果我使用这样的东西,它可以工作并将数据发送到控制台:

工作 HTML 代码:

<p class="text-center itemName">click to edit this text</p>

工作 jQuery 代码:

$('.itemName').editable({
                                        type: 'text',
                                          pk: 1,
                                         url: 'update.php',
                                     send: 'always'
                                    });

非工作 jQuery:

$.fn.editable.defaults.mode = 'popover';

$('.tags').editable({
    placement: 'right',
    select2: {
        tags: ['cake', 'cookies'],
        tokenSeparators: [",", " "]
    },
    display: function(value) {
        $.each(value,function(i){
           // value[i] needs to have its HTML stripped, as every time it's read, it contains
           // the HTML markup. If we don't strip it first, markup will recursively be added
           // every time we open the edit widget and submit new values.
           value[i] = "<span class='label'>" + $('<p>' + value[i] + '</p>').text() + "</span>";
        });
        $(this).html(value.join(" "));
    }
});

$('.tags').on('shown', function() {
    var editable = $(this).data('editable');
    value = editable.value
    $.each(value,function(i){
       value[i] = $('<p>' + value[i] + '</p>').text()
    });
});

$('[id^="tags-edit-"]').click(function(e) {
    e.stopPropagation();
    e.preventDefault();
    $('#' + $(this).data('editable') ).editable('toggle');
});

$.mockjax({
    type: 'text',
                                              pk: 1,
                                             url: 'update.php',
                                         send: 'always'
});

update.php

<?php
    /*
    Script for update record from X-editable.
    */

    //delay (for debug only)
    sleep(1); 

    /*
    You will get 'pk', 'name' and 'value' in $_POST array.
    */
    $pk = $_POST['pk'];
    $name = $_POST['name'];
    $value = $_POST['value'];

    /*
     Check submitted value
    */
    if(!empty($value)) {
        /*
          If value is correct you process it (for example, save to db).
          In case of success your script should not return anything, standard HTTP response '200 OK' is enough.

          for example:
          $result = mysql_query('update users set '.mysql_escape_string($name).'="'.mysql_escape_string($value).'" where user_id = "'.mysql_escape_string($pk).'"');
        */

        //here, for debug reason we just return dump of $_POST, you will see result in browser console
        print_r($_POST);


    } else {
        /* 
        In case of incorrect value or error you should return HTTP status != 200. 
        Response body will be shown as error message in editable form.
        */

        //header('HTTP 400 Bad Request', true, 400);
        echo "This field is required!";
    }

?>
4

1 回答 1

0

OP 找到解决方案,就在这里。

$.fn.editable.defaults.mode = 'popover';

$('.tags').editable({

    placement: 'top',
    select2: {
        tags: ['cookies', 'pie','cake'],
        tokenSeparators: [","]
    },
    display: function(value) {
        $.each(value,function(i){
           value[i] = "<span class='tagBox'>" + $('<p>' + value[i] + '</p>').text() + "</span>";
        });
        $(this).html(value.join(" "));
    }
});

$('.tags').on('shown', function() {
    var editable = $(this).data('editable');
    value = editable.value
    $.each(value,function(i){
       value[i] = $('<p>' + value[i] + '</p>').text()
    });
});

$("#content").on("click", '[id^="tags-edit-"]', function(e) {

    e.stopPropagation();
    e.preventDefault();

    $('#' + $(this).data('editable') ).editable('toggle');
});


$('.tags').on('save', function(e, params) {
    var itemId = $(this).attr('data-pk');
    var dataString = 'value='+ params.newValue +'&id='+ itemId;

            $.ajax({
            type: 'POST',
                        data: dataString,
                         url: 'update.php',
                        send: 'always'
                });

});
于 2015-11-03T20:49:31.470 回答