2011-09-24 149 views
1

如何將此javascript選擇轉換爲jQuery?我需要選擇一個按鈕類而不是一個ID,並且我理解最簡單的方法是jQuery。將JavaScript函數轉換爲jQuery

var playButton1 = document.getElementById("playButton1"); 

playButton1.onclick = function() { 
    var ta = document.getElementById("playButton1"); 

    document['player'].receiveText1(ta.value); 

    ta.value = "" 
}; 
+3

你看了一個jQuery教程?問題是什麼? –

+1

有點偏離主題,但爲什麼你有變量'ta'?你不能只用你的'playButton1'變量嗎? –

+1

很多答案在這裏! –

回答

2

嘗試以下。你沒有指定新的類名,所以我認爲它是「playButton」。

$(document).ready(function() { 
    $('.playButton').click(function() { 
    document['player'].receiveText1(this.value); 
    this.value = ""; 
    }); 
}); 
0
var playButton1 = $("playButton1"); 

playButton1.click(function() { 
    var ta = $("playButton1"); 
    document['player'].receiveText1(ta.val()); 
    ta.val(""); 
}); 
0
var playButton1 = $("#playButton1"); 

playButton1.onclick = function() { 
    var ta = playButton1.val(); 
    document['player'].receiveText1(ta); 
    playButton1.val(''); 
}; 
0
$('#playButton1').click(function(){ 
    document['player'].receiveText1($('playerButton1').val()); 
    $('playerButton1').val(''); 
}); 
0
<button class="playButton" value="1">Play 1</button> 
<button class="playButton" value="2">Play 2</button> 
<script type="text/javascript"> 
    $(function(){ 
     $(".playButton").click(function(){ 
      document['player'].receiveText1($(this).val()); 
     }); 
    }); 
</script> 
0
$('#Class').click(function(e){ 
    document['player'].receiveText1(event.target.value); 
    event.target.value = "" 
});