2

ボタンがクリックされると、 onclick 属性を使用して "enableEditing" 関数が呼び出されると、テーブルは私のサイトのデータベースから取得したデータを示しています。次に、テーブルの各行の 1 つのフィールドに対して、フィールドを値として使用し、キーを入力名として使用する入力テキスト ボックスが表示されます。

前:

<tr class="data_row">
    <td class="property_cell">project ref</td>
    <td class="myNumber">200407</td>
</tr>

後:

<tr class="data_row">
    <td class="property_cell">project ref</td>
    <td class="myNumber">
        <input name="project ref" value="200407">
    </td>
</tr>

jQuery:

function enableEditing(){

    $("#object_data tr").each(function(){
        var key = $(this).children(".property_cell").html();
        var value = $(this).children(".myNumber").text();
        $(this).children(".myNumber").html("<input name='" + key + "' value='" + value + "'>");
    });
}

これは問題なく動作しますが、データベースのデータの一部にスピーチ マークや一重引用符が含まれており、入力フィールドに変更すると、入力 html が台無しになります。各入力フィールドの html をエスケープするにはどうすればよいですか?

4

3 に答える 3

2

試す

$('.myNumber').contents().replaceWith(function(){
    return $('<input />', { name: $(this).parent().prev().text(), value : $(this).text()});
})

デモ:フィドル

于 2013-06-12T13:20:03.027 に答える