选择第1页上的item2将返回第2页上的item1

时间:2016-01-27 21:42:35

标签: javascript jquery

我从数据库动态创建行和表以显示只读项列表。单击其中一个项目时,页面应重新加载到其他窗口并显示与所单击项目相关的相关结果。

我有99%的工作,但是当我点击第一行时(让我们说“item1”,下一个窗口反映未定义。当我点击第二行(“item2”)时,窗口反映了item1的结果。我点击第三行(“item3”),窗口反映了item2的结果。

我最了解的是我可能没有正确存储变量。

我最接近的猜测是这可能是queryString的解决方案,但我不确定如何或在何处实现它。

为了可能遇到此问题的其他人,我提供了所有相关代码。

var customApp = function(){

// Establish Variables
var salonName
var salonDomainLink
var salonAddress
var salonPhoneNumber
var salonSelection

// Queries Salons class
var query1 = new Parse.Query("Salons");
query1.find({
    success: function(results) {

    // Changes search results from JSON Object to ?something readable?
        var searchResults = JSON.parse(JSON.stringify(results))

        // Loops through every result and creates variables with each specific result
        for ( var i in searchResults) {
            salonName = searchResults[i].name;
            salonDomainLink = searchResults[i].domainLink;

            // Creates a new row for each item in table
            var newrow = $('<tr>').text('').appendTo('#searchTable');

            // Adds a cell for each salonName in row
            var nameCell = $('<td>').text(salonName).appendTo(newrow);

            // Adds an id of the salon name + 'Id' to each row
            var cellId = nameCell.attr("id", salonName.replace(/\s+/g, '') + "Id")

            // Changes class of each cell
            nameCell.addClass("text-left font-w600 text-primary");
        }

        // Passes clicked salon name to a variable for use on another page
        $('#searchTable tr td').click(function(){

            // Acquires text value of the specific td of tr
            salonSelection = $(this)[0].childNodes[0].nodeValue

            // Saves the variable to local storage (can be retrieved with "localStorage.salonSelection")
            delete localStorage.salonSelection;
            window.localStorage.salonSelection = salonSelection;
            window.location = 'base_pages_schedule_1.html';
        });
    },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message + ". Please contact Support.");
    }
});

query1.equalTo("name", localStorage.salonSelection);
query1.find({
    success: function(results) {
        var searchResults = JSON.parse(JSON.stringify(results))
        // Loops through every result and creates variables with each specific result
        for ( var i in searchResults) {
            window.localStorage.salonName = searchResults[i].name;
            window.localStorage.salonDomainLink = searchResults[i].domainLink;
            window.localStorage.salonAddress = searchResults[i].address;
            window.localStorage.salonPhoneNumber = searchResults[i].phoneNumber;
        }
    },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message + ". Please contact Support.");
    }
});
$('#salon-name').text(localStorage.salonName);
$('#salon-address').text(localStorage.salonAddress);
$('#salon-phone-number').text(localStorage.salonPhoneNumber):};

1 个答案:

答案 0 :(得分:0)

query.find()是异步的,因此在您在dom中设置text()之前,它不会设置新值

将dom更新移至find()成功回拨

query1.find({
    success: function(results) {
        var searchResults = JSON.parse(JSON.stringify(results))
        // process results

        // then update dom
        $('#salon-name').text(localStorage.salonName);

    },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message + ". Please contact Support.");
    }
});

另请注意,您的for循环会在循环的每次迭代中为存储键重写一个新值,因此只会存储最后一个。不确定您的存储目标是什么