[英]How to Display Specific value when I click Specific button
When I click all the button output will be same (ie 1).I want to display specific value of specified button for EXAMPLE:- When I click button 1 then It must display output as 1 and when I click 2 it must display output as 2.please help me. 当我点击所有按钮输出将是相同的(即1)。我想显示指定按钮的具体值为例子: - 当我点击按钮1然后它必须显示输出为1,当我点击2它必须显示输出为请帮助我。
<?php
for ($i=0; $i < 10 ; $i++) {
echo '<br><button value="'.$i.'" class="my_remark_btn">'.$i.'</button>';
}
?>
<script type="text/javascript">
$('.my_remark_btn').click(function(){
remark_aftr_click();
});
function remark_aftr_click(){
var k = $(".my_remark_btn").val();
$.ajax({url: "my_remark_act.php",
cache: true,
type: "POST",
data: {go:k},
success: function(data_remark_mi){
alert(data_remark_mi);
}
});
}
</script>
my_remark_act.php:- my_remark_act.php: -
<?php echo $_POST['go']; ?>
Try This Example. 试试这个例子。
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$( document ).ready(function() {
$("button").click(function() {
alert(this.id); // or alert($(this).attr('id'));
});
});
</script>
</head>
<body>
<button id="some_id1"></button>
<button id="id2"></button>
<button id="id3"></button>
<button id="id4"></button>
<button id="id5"></button>
</body>
</html>
$('body').append("<button data-value='val' class='my_remark_btn'>val</button>") $(document).on('click', '.my_remark_btn', function() { alert($(this).attr('data-value')) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
value
. 按钮没有value
。 data-value
. 使用数据属性,如data-value
。 $(this).attr('data-value')
using this context for clicked button. 使用此上下文为点击按钮获取$(this).attr('data-value')
。 Have a try of this 试一试
jQuery jQuery的
$('.my_remark_btn').click(function(){ // when a button is clicked
var value = $(this).value; // get the value of that specific button
alert(value); // alert the value to the page (to make sure it is correct)
remark_aftr_click(value); // run function, with value as a parameter
});
function remark_aftr_click(val){
$.ajax({
url: "my_remark_act.php",
cache: true,
type: "POST",
data: {go: val}, // post data to Ajax with CORRECT value
success: function(data){
alert(data); // alert returned data
}
});
}
Why your code was not working : 为什么你的代码不起作用 :
When you were retrieving the value
of .my_remark_btn
before, you were only ever getting the value
of the first element with that specific class. 当您之前检索.my_remark_btn
的value
时,您只获得具有该特定类的第一个元素的value
。
How you can get it to work : 如何让它工作 :
Instead, you need to pass the value
as a parameter within your function
(while using this
as a selector to get the value of that specific button). 相反,您需要将value
作为function
的参数传递(同时将this
用作选择器以获取该特定按钮的值)。
Hope this helps :-) 希望这可以帮助 :-)
You can use JQuery. 你可以使用JQuery。 try the below code. 试试下面的代码。
// this will fire for all button having class '.my_remark_btn'
$('.my_remark_btn').click(function(){
// using $(this) you will get the current element
var value = $(this).val();
console.log(value);
});
In you code you are accessing value by var k = $(".my_remark_btn").val();
在您的代码中,您通过var k = $(".my_remark_btn").val();
来访问值var k = $(".my_remark_btn").val();
, This will only return value of the first
button with class my_remark_btn
. ,这只会返回类my_remark_btn
的first
按钮的值。
You need $(this).attr("value")
like below:- 你需要$(this).attr("value")
如下所示: -
<script type="text/javascript">
$('.my_remark_btn').click(function(){
var k = $(this).attr("value");
$.ajax({url: "my_remark_act.php",
cache: true,
type: "POST",
data: {go:k},
success: function(data_remark_mi){
alert(data_remark_mi);
}
});
});
</script>
Example:- 例:-
$('.my_remark_btn').click(function(){ var k = $(this).attr("value"); alert(k); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button value ="1" class="my_remark_btn">1</button><br> <button value ="2" class="my_remark_btn">2</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.