简体   繁体   中英

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.

在此输入图像描述

<?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:-

<?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> 

  1. Button has no value .
  2. Use data attribute like data-value .
  3. Get it like $(this).attr('data-value') using this context for clicked button.
  4. Use event delegation for dynamically added elements.

Have a try of this

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.

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).

Hope this helps :-)

You can use 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(); , This will only return value of the first button with class my_remark_btn .

You need $(this).attr("value") like below:-

<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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM