ボタンのクリックで外部呼び出しを行い、成功を反映するようにボタンを更新するページがあります。ajax 呼び出しは適切に機能しますが、ページに多数のボタンがある場合、ボタンのテキストを操作するのが困難です。
それがページ上の唯一の項目である場合は簡単に照合でき $(".sabPauRes.ui-btn-text").text("Updated");
ますが、each 関数を使用しているときに $(this) を使用してそれを指す方法がわかりません。「最も近い」について少し読みましたが、私が望むことを達成していないようです(または単に間違っているだけです)。
以下コードサンプル!
$(document).ready(function(){
$('.sabPauRes').each(function() {
$(this).click(function(event) {
event.preventDefault();
$.ajax({
type: "GET",
url: this.href,
cache: false,
dataType: "text",
success: onSuccess
})
})
$("#resultLog").ajaxError(function(event, request, settings, exception) {
$("#resultLog").html("Error Calling: " + settings.url + "<br />HTTP Code: " + request.status);
})
function onSuccess(data)
{
// validate the result of the ajax call, update text, change button methods as needed
if (data == "Success") {
// PROBLEM -- how do I use $this to match a class that is nested within it?
$(this).closest(".ui-btn-text").text("Updated");
} else {
alert("Failed: " + data);
}
$("#resultLog").html("Result: " + data);
}
})
})
html
<body>
<div data-role="page">
<div data-role="content">
<div data-role="collapsible">
<h3>This is an item</h3>
<p>
<a href="/api/?resume=fhdh54" class="sabPauRes" data-ajax="false" data-role="button" data-inline="true" data-request="resume">Resume Download</a>
<div id="resultLog"></div>
</p>
</div>
</div>
</body>