JQUERY
JQUERY
When I clicked the both 2 buttons it only returns the value 1
当我点击两个按钮时,它只返回值1
$(document).ready(function() {
var getvalue = $(".view_btn").val();
$(".view_btn").click(function() {
alert(getvalue);
});
});
PHP
PHP
<?php foreach ($studentRankingViewGET as $studentRankingViewSHOW) {?>
<input type="button" value="<?php echo $studentRankingViewSHOW['id'];?>" class="view_btn">
<?php } ?>
This returned 2 values .i.e. 1 AND 2
这返回了两个值。1和2
3 个解决方案
#1
2
$('.view_btn').click(function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="button" value="123" class="view_btn"/>
<input type="button" value="456" class="view_btn"/>
you should use $(this) as referring object.
您应该使用$(this)作为引用对象。
#2
0
Since you have two buttons with same class
. You can get the clicked element with this
.
因为您有两个具有相同类的按钮。您可以用这个获取被单击的元素。
$(".view_btn").click(function() {
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="view_btn" value="Button 1">Button 1<button>
<button class="view_btn" value="Button 2">Button 2<button>
#3
0
This will support for dynamic content also
这也将支持动态内容
$(".view_btn").on("click", function() {
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="view_btn" value="Button 1">Button 1<button>
<button class="view_btn" value="Button 2">Button 2<button>
#1
2
$('.view_btn').click(function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="button" value="123" class="view_btn"/>
<input type="button" value="456" class="view_btn"/>
you should use $(this) as referring object.
您应该使用$(this)作为引用对象。
#2
0
Since you have two buttons with same class
. You can get the clicked element with this
.
因为您有两个具有相同类的按钮。您可以用这个获取被单击的元素。
$(".view_btn").click(function() {
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="view_btn" value="Button 1">Button 1<button>
<button class="view_btn" value="Button 2">Button 2<button>
#3
0
This will support for dynamic content also
这也将支持动态内容
$(".view_btn").on("click", function() {
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="view_btn" value="Button 1">Button 1<button>
<button class="view_btn" value="Button 2">Button 2<button>