1

現在、タグid内の値を使用しています to 、つまり:divserialize

HTML:

<div class="column">
    <div id="portlet_1">some content here</div>
    <div id="portlet_2">some content here</div>
    <div id="portlet_3">some content here</div>
</div>

jQuery:

$(this).sortable('serialize', {key: 'item'})

それはうまくいきます。

問題は、必要に応じて同じポートレットを何度も画面に表示できるように、ユーザーが画面に表示するポートレットを動的に選択できるようにする必要があることです。つまり、次のようになります。

HTML:

<div class="column">
    <div id="portlet_1">some content here</div>
    <div id="portlet_1">some content here</div>
    <div id="portlet_1">some content here</div>
    <div id="portlet_2">some content here</div>
    <div id="portlet_2">some content here</div>
    <div id="portlet_3">some content here</div>
</div>

これは、同一idの s のために多くの問題を引き起こします。

s のclass代わりに esを使用するように構造を変更したので、次のようになります。id

HTML:

<div class="column">
    <div class="portlet_1">some content here</div>
    <div class="portlet_1">some content here</div>
    <div class="portlet_1">some content here</div>
    <div class="portlet_2">some content here</div>
    <div class="portlet_2">some content here</div>
    <div class="portlet_3">some content here</div>
</div>

classの代わりに に基づいてシリアル化するにはどうすればよいidですか?

id以下は、 sに基づいてシリアル化されるために機能しないコードのより完全な抜粋です。

jQuery:

$(".column").sortable({
    connectWith: '.column',
    update: function(event, ui) {
        var that = this;

        $.ajax({
            url: 'some web service here',
            type: 'POST',
            data: { strItems:$(that).sortable('serialize', {key: 'item'}) },
            error: function(xhr, status, error) {
                //some error message here
            },
            success: function() {
                //do something on success
            }
        });

    }
});

申し訳ありませんが、私は古い jquery バージョン 1.4 を使用していることを忘れていました。

4

3 に答える 3

2

これを試して :

$(this).sortable('serialize', {key: 'item', attribute: 'class'});

jQuery UI 1.8ドキュメント:

可能なオプションは次のとおりです: 'key' (part1[] を必要なものに置き換えます)、'attribute' ('id' 以外の別の属性をテストします) および 'expression' (独自の正規表現を使用します)。

于 2012-06-20T11:06:45.683 に答える
0

手でそれを行うことができます:

data: function(){
    var mySerial;
    $(this).find('div').each(function(){
        var raw = $(this).attr('class');
        var el = raw.split('_');
        mySerial += el[0]+"[]="+el[1]+"&";
    });
    return mySerial.substr(0,(mySerial.length-1));
}

これは次のようなものを返すはずです: portlet[]=1&portlet[]=1&portlet[]=2...

于 2012-06-20T11:11:50.300 に答える
0

この要旨。

jQuery('.class_input').serializeAnything()

https://gist.github.com/4482228

(function($) {

$.fn.serializeAnything = function() {

var toReturn = [];

$.each(this, function() {

if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type))) {

var val = $(this).val();

toReturn.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( val ) );

}

});

return toReturn.join("&").replace(/%20/g, "+");

}


})(jQuery);
于 2013-01-08T08:46:17.550 に答える