2017-05-09 25 views
-1

在else語句下,我無法弄清楚如何將userChoice的值傳遞給jquery對象「.unit」,以便存儲在.unit中的div的數量由給定的數量決定由用戶。 $ pallet在html中作爲div容器存在。任何幫助,將不勝感激!用戶值傳入jquery對象

$(document).ready(function() { 
    for (var x = 0; x < 16; x++) { 
    for (var y = 0; y < 16; y++) { 
     var unit = $("<div class='unit'></div>"); 
     unit.appendTo('#pallet'); 
    } 
    } 
    $(".unit").hover(function() { 
    $(this).css("background-color", "black"); 

    }); 
    document.getElementById("reset").onclick = gridReset; 

    function gridReset() { 
    var userChoice = prompt("Between 1 and 64 how big would you like your grid?"); 

    if (isNaN(userChoice)) { 

     alert(userChoice + " is not a number"); 

    } else if (userChoice >= 65 || userChoice <= 0) { 

     alert("Choose a number between 1 and 64"); 

    } else { 

     $(".unit").css("background-color", "blue"); 
    } 
    } 
}); 
+0

在任何地方撥打這個*「我無法弄清楚如何通過userChoice的價值到jquery對象「.unit」中,以便存儲在.unit中的div的數量由用戶給出的數字決定。「*沒有做出一堆假設就沒有多大意義。你說什麼號碼?創建一個新的網格?哪裏可以做到這一點(或者你想用這個數字來做什麼)的代碼在哪裏? –

回答

0

你可以簡單地在你的div裏面設置隱藏的輸入,你可以保留這個值。

var unit = $("<div class='unit'><input type='hidden' class='unit-value'/></div>"); 

和你else語句中:

$(".unit").css("background-color", "blue"); 
$(".unit-value").val(userChoice); 
-1

使用$(".unit").data("userChoice", userChoice);那麼你可以爲var userChoice = $(".unit").data("userChoice");