var el = $(this); // it's a form
ある時点で:
el.replaceWith('<p>Loading...</p>');
後で:
el.replaceWith(output);
どうやらelは最初のreplaceWithの後、もう存在しません...
どうにかしてel
、明らかに新しいコンテンツを維持できますか?
var el = $(this); // it's a form
ある時点で:
el.replaceWith('<p>Loading...</p>');
後で:
el.replaceWith(output);
どうやらelは最初のreplaceWithの後、もう存在しません...
どうにかしてel
、明らかに新しいコンテンツを維持できますか?
オリジナルel
は削除され、 に置き換えられましたreplaceWith
。の戻り値を使用して、新しい参照を作成しますreplaceWith
。
var el = $(this); // it's a form
el = el.replaceWith('<p>Loading...</p>');
//`el.replaceWith()` returns the new element
el = el.replaceWith(output);
フォームを維持したまま、内部コンテンツを新しい要素に置き換える場合は、次を使用します。
el.html(""); //Clear html
el.append('<p>Loading...</p>');
jquery の replaceWith は、新しい要素ではなく、置き換えられた要素を返します
http://api.jquery.com/replaceAll/
次のようにする必要があります。
var el = $(this); // it's a form
var $p = $('<p>Loading...</p>');
el.replaceWith($p);
// now el is unhooked from the dom, that is el.parent() === []
// it is $p who is now hooked, check with $p.parent() === (some node)
//so you should reassing it
el = $p;
// later on
el.replaceWith(output); // but remenber, once again el is unhooked
また、この質問を確認してください: https://stackoverflow.com/a/892915/47633