Я использую jQuerymobile, и у меня есть список. В представлении списка у меня есть динамический контент, загружаемый из localStorage. Когда вы щелкаете по определенному элементу списка, я хочу, чтобы пользователь перешел на следующую страницу, связанную с этим конкретным объектом localstorage.Можно ли загружать и комбинировать динамические строки в localStorage.getItem()?
Я могу как-то заставить мой код работать ниже (что я прокомментировал). Проблема заключается в том, что
var favoritedName = JSON.parse(localStorage.getItem(placesText));
возвращает NULL.
Но если я вручную загрузить в
var favoritedName = JSON.parse(localStorage.getItem("placesFireside Bar"));
он работает отлично.
Мне интересно, можете ли вы динамически загружать в .text() из списка, который вы нажимаете в списке, а затем задаете это как ключ в localstorage.getItem().
Вот мой код:
// click the list item, which is a specific bar
$('#all-my-favorites li').click(function() {
// get specific name of Bar from the list item
listitemText = $(this).text();
// OR manually name the list item. example here is "Fireside Bar" for testing purposes
//listitemText = "Fireside Bar";
// combine the strings, so i can search localstorage for the relevant key item
placesText = "places" + listitemText;
//alert(placesText);
// returns "placesFireside Bar" or "places(barname from listitem)"
});
// go to the new page, which should show content from the relevant key item
$('#favorited').on('pagebeforeshow', function() {
// test to see if placesText is a string
alert(jQuery.type(placesText) === "string");
// returns TRUE
// find the relevant keyitem from localstorage, and name the object favoritedName
var favoritedName = JSON.parse(localStorage.getItem(placesText));
alert(favoritedName);
// returns NULL
$(this).find('[data-role=header] .ui-title').text(favoritedName.name);
$('#FavoritedplaceName').text(favoritedName.name);
$('#FavoritedlocationName').text(favoritedName.location);
$('#FavoritedtimeofDay').text(favoritedName.timeOfDay);
$('#FavoriteddayofWeek').text(favoritedName.dayofWeek);
});
Если вы установили console.log (placesText) после того, как вы его установили, что вы получаете? – K3N
@ Ken-AbdiasSoftware Я получаю правильный вывод, который является «placesFireside Bar», который представляет собой текст элемента списка() «Fireside Bar» + «places» – Aaron