2014-04-12 36 views
0

我的第一个问题。我有一种感觉,这是简单的,但它让我疯狂,因为我找不到问题。我已经用警报测试了数学函数,所以我可以看到这是工作,但下面的实际jQuery似乎并没有运行交换文本。请帮忙!骰子jQuery - 随机#+替换死亡文字

var dice = 1; 
function math(sides){ 
    return Math.floor((Math.random()*sides)+1).toString(); 
} 

$(document).ready(function(){ 
    $("button").click(function(){ 
     var dice = math(6); 
     alert("Dice now at " + dice); 
     $("#number").text("dice"); 
    }); 
}); 
+2

你把引号的文本功能的霸气。然而,骰子是一个变量,只是删除quoutes,一切都会工作/ –

回答

2

骰子是一个变量,因此将其删除引号

试试这个

$("#number").text(dice); // This will replace the current text with new 

如果#number是文本框

$("#number").val(dice); // This will replace the current value with new 
+1

+1 #number是一个'textbox'?那么它必须是'$(“#number”)。val(骰子);' – Praveen

+0

@Praveen更新谢谢';-)' –

+0

谢谢。我尝试了两个版本,但仍然不工作: -/ '$(“#number”)。text(dice);' '$(“#number”)。val(dice);' – Superman2971