简体   繁体   English

单击“特定”按钮时如何显示特定值

[英]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> 

  1. Button has no value . 按钮没有value
  2. Use data attribute like data-value . 使用数据属性,如data-value
  3. Get it like $(this).attr('data-value') using this context for clicked button. 使用此上下文为点击按钮获取$(this).attr('data-value')
  4. Use event delegation for dynamically added elements. 使用事件委托来动态添加元素。

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_btnvalue时,您只获得具有该特定类的第一个元素的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_btnfirst按钮的值。

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.

相关问题 当我使用javascript单击按钮时,如何在另一页上显示特定图像? - How to make a specific image display on another page when I click on a button using javascript? 我想点击一个按钮并在 React 中显示一个特定的组件 - i want to click a button and display a specific component in React 单击时如何获取特定的文本值? - How to get a specific text value when click? <button>单击按钮时,如何在 console.log 中显示特定文本?</button> - How do I make a <button> show a specific text in console.log when I click on the button? 如何通过单击按钮显示特定div id中的数据数组? - How can I display array of data in specific div id by click in a button? 除非用户单击特定的按钮,否则如何向用户显示警报框 - How can I display alert box to users unless they click a specific button 如何使用jQuery Bootstrap在按钮单击上显示特定的div - how to display specific div on button click using jquery bootstrap 如何在单击按钮时显示特定ACF行的内容 - How to display the content of a specific ACF row on button click jquery 点击按钮显示具体模板 - jquery click on button display specific template 使用 JavaScript 在控制台中单击任何单选按钮时如何显示特定值? - How to show specific value when click on any radio button in console using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM