0

関数を呼び出そうとしていますが、値を適切にエスケープして適切に渡すのに苦労しています。私は現在持っています:

function selectdone(sel, title_id, status_type) {
...
}

$(function() {


$("td.status-updates").click(function() {
    if ($(this).find('#sel').length == 0) {
        var before = $(this).text();
        var title_id = $(this).parent().attr('id');
        var status_type = $(this).attr('class');
        $(this).html("<select id='sel' 
            onchange='selectdone(this," + title_id + "," + status_type +");'...
                                                <option>NS</option></select>");
    }

});

私がこれから得続けるエラーはですUncaught SyntaxError: Unexpected identifier

ただし、正常に'selectdone(this," + title_id + ");...機能するようにパスすると、3つパスしようとすると、そのエラーが発生します。

注:status_type変数(複数のクラス)にはスペースがあります。

4

2 に答える 2

2

jQueryには、イベントを処理し、DOMを操作するための優れた組み込みツールがあります。それらを使用することをお勧めします。

$("td.status-updates").click(function() {
    if ($(this).find('#sel').length == 0) {
        var before = $(this).text();
        var title_id = $(this).parent().attr('id');
        var status_type = $(this).attr('class');
        $(this).empty().append(
            $('<select>').prop('id', 'sel')
            .on({
                change: function() {
                    selectdone(this, title_id, status_type);
                }
            })
            .append($('<option>').text('NS'))
        );
    }
});

関連するブログ投稿

于 2012-06-26T23:12:43.767 に答える
1

あなたの最後の質問から自分自身を繰り返すために:

$(this).html($("<select/>", {
  id: 'sel',
  change: function() {
    selectdone(this, title_id, status_type);
  }
}).append($("<option/>", { text: "NS" })));

また、「クラス」を取得するには、「。prop()」を使用することをお勧めします。

var status_type = $(this).prop('className');

プロパティとしては「className」です。jQuery 1.6以降、「。prop()」ではなく「.attr()」が本当に必要になることはほとんどありません。

于 2012-06-26T23:13:30.757 に答える