-1

我不知道... :-( 如何选择具有名称 + 变量的元素(对应的注释 id)

HTML:

<div class="editable" id="tiny_12">blablabla</div>
<input type="button" class="bouton_save_ajax" id="sauver_billet_html_12">

<div class="editable" id="tiny_13">blablabla</div>
<input type="button" class="bouton_save_ajax" id="sauver_billet_html_13">

<div class="editable" id="tiny_14">blablabla</div>
<input type="button" class="bouton_save_ajax" id="sauver_billet_html_14">

...
...

和 javascript (Jquery):

$(document).ready(function () {

    $("#sauver_billet_html_12").click(function (event) {

        var bufferId = {};
        bufferId['html'] = $("#tiny_12").html();
        // bufferId['id'] = 12;

        $.ajax({
            type: "POST",
            url: "ajax_save_post",
            data: bufferId,
            dataType: "html",
            success: function (data) {
                alert(event.target.id);
            }
        });
    });
});

谢谢

4

1 回答 1

1

我想你想要这样的东西:

http://jsfiddle.net/WHL2P/1

$(document).ready(function () {

    $("input[id^=sauver_billet_html_]").click(function (event) { 
        var buffer = {};
        buffer.id = this.id.split('_').pop();
        buffer.html = $("#tiny_"+buffer.id).html();

        $.ajax({
            type: "POST",
            url: "ajax_save_post",
            data: buffer,
            dataType: "html",
            success: function (data) {
                alert(event.target.id);
            }
        });
    });
});
于 2013-06-10T17:32:11.373 回答