私はしばらくこれに取り組んでいて、すべてを試しました (そして、stackoverflow のおかげで多くの新しいことを学びました)。私が遭遇し続ける問題は、citations.js から保存されたクリック ハンドラーから値を取得できないように見えるので、main.js で使用できるようになることですか? set と get を使用して関数を使用してみました。クリック ハンドラー内で ajax 呼び出しを返したり、他の多くのものを返したりしたので、どんな助けも大歓迎です。
request.js、citations.js、main.js の 3 つの .js ファイルがあります。
これがコードです。
request.js,
function citeAjax(citationId) {
return $.ajax({
type: 'GET',
url: 'https://www.sciencebase.gov/catalog/itemLink/' + citationId +
'?format=jsonp',
jsonpCallback: 'getSBJSON',
contentType: "application/json",
dataType: 'jsonp'
});
}
var promise = citeAjax(citationId);
citations.js,
promise.done(function (json) {
var linkBase = "http://www.sciencebase.gov/catalog/item/";
var link = "";
var itemId = "";
var urlId = "";
$.each(json.items, function(i,item) {
link = linkBase + this.id;
$('#sbItems').append('<li><b><a href="' + link + '" id="idNum' +
i + ' ">' + this.title + '</a> - </b>' + this.summary + '</li>');
});
$('#sbItems a').click(function () {
var str = $(this).attr('id');
if (str.length == 7) {
itemId = str.slice(5,6);
} else if (str.length == 8) {
itemId = str.slice(5,7);
}
**// I want to get (json.items[itemId].id) which is any id from the
link that is clicked and then store it in citeAjax()**
}); // END Click event
}).fail(function() {
alert("Ajax call failed!");
});
main.js、
使いたい
promise.done(function (json) {
// do something with the ajax request from any id that's inserted into the url
});
助けてくれてありがとう