2017-01-14 59 views
0

我試圖使某個文本出現時點擊一個元素時,該按鈕有一定的文字。但是第一個if語句的相同文本不斷出現。檢查按鈕值jquery

這是按鈕:

<button class="game-button">Start spel</button> 

這些表元素:

<div id="speelveld"> 
      <table border="0"> 
       <tr> 
        <td><img alt="" title="" src="img/empty.jpg" /></td> 
        <td><img alt="" title="" src="img/empty.jpg" /></td> 
        <td><img alt="" title="" src="img/empty.jpg" /></td> 
       </tr> 
       <tr> 
        <td><img alt="" title="" src="img/empty.jpg" /></td> 
        <td><img alt="" title="" src="img/empty.jpg" /></td> 
        <td><img alt="" title="" src="img/empty.jpg" /></td> 
       </tr> 
       <tr> 
        <td><img alt="" title="" src="img/empty.jpg" /></td> 
        <td><img alt="" title="" src="img/empty.jpg" /></td> 
        <td><img alt="" title="" src="img/empty.jpg" /></td> 
       </tr> 
      </table> 
     </div> 

,這是javascript代碼:

$(".game-button").click(function(){ 
    $(".game-button").html("Reset spel"); 
}); 

if ($(".game-button").val() == "Start spel") { 
    $("#speelveld tr td").click(function() { 
     alert("Je kunt nog niet beginnen"); 
    }); 
} else { 
    $("#speelveld tr td").click(function() { 
     alert("hoi"); 
    }); 
} 

我的問題是:我怎麼能當按鈕有一定的文字時會出現一定的提示

+1

你要綁定的'if'內部事件。所以只有'if'裏面的事件被註冊 –

回答

1

您正在根據從未滿足的條件在td上設置處理程序。

修改您的處理程序以在每次單擊表格的一個單元格時檢查按鈕的值。

$(".game-button").click(function(){ 
    $(".game-button").text("Reset spel"); 
}); 

$("#speelveld tr td").click(function() { 
    if ($(".game-button").text() === "Start spel") {  
     alert("Je kunt nog niet beginnen"); 
    } 
    else { 
     alert("hoi"); 
    } 
}); 
0

您在if內部綁定事件。因此,只有內部if事件被註冊

這就是你需要做什麼:

$("#speelveld tr td").click(function() { 
     if ($(".game-button").html() == "Start spel") { 
     alert("Je kunt nog niet beginnen"); 
     } else {    
     alert("hoi");    
     } 

    });