如何使用Javascript获取按钮值 [英] How to get button value using Javascript

查看:115
本文介绍了如何使用Javascript获取按钮值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用JavaScript获取按钮的值.在PHP代码中使用while语句生成了多个按钮,因此nameid对于所有按钮都是相同的.它们之间的唯一区别是值.

I am trying to figure out how to get the value of a button using JavaScript. There are several buttons generated using a while statement in php code, so the name and id are the same for all of them. The only difference between them is the value.

我的php代码的一部分:

Part of my php code:

while($row = mysql_fetch_array($result))
     {
     echo "<tr>";
     echo "<td align=\"center\"><input type=\"button\" id=\"details\" name=\"details\" value=\"" . $row['mcn'] . "\" onClick=\"MCNdetails()\"></td>";
     echo "</tr>";
     }

我想要实现的是,当单击此按钮时,将显示一个JavaScript警报,其中包含该记录的详细信息.

What I'm trying to achieve is that when this button is clicked a JavaScript alert is displayed with this records details.

(FYI-"Faker"是该表所属的表单名称.)

(FYI - "Faker" is the form name that this table is a part of.)

我的JavaScript代码:

My JavaScript code:

function MCNdetails()
        {

        var buttonDetails = document.Faker.details;
        var buttonValue = buttonDetails.value;

        if (buttonValue === "2700-2057" )
            {
            alert ("Details are here");
            return;
            }
        else
            {
            alert ("No Dice " + buttonValue);
            return;
            }

        }

我大约有.到目前为止,有300条记录.当我单击任何一个按钮时,我总是会得到else语句.我确实尝试过通过id来获取元素,例如:

I have approx. 300 records so far. When I click on ANY of the buttons I always get the else statement. I did try getting the element by id like this:

function MCNdetails()
        {

        var buttonDetails = document.getElementById("details");
        var buttonValue = buttonDetails.value;

        if (buttonValue === "2700-2057" )
            {
            alert ("Details are here");
            return;
            }
        else
            {
            alert ("No Dice " + buttonValue);
            return;
            }

        }

但是,无论我单击哪个按钮,它始终会返回第一个按钮的值.

But that would always return the value of the first button regardless of which one I clicked.

我已经完成了一些Google搜索以及该网站的搜索,但在生成按钮的同一场景中找不到.

I have done some Google searching as well as searching this site and I can't find this same scenario where the buttons are generated.

任何帮助或指点将不胜感激.谢谢您的时间:)

Any help or pointers would be appreciated. Thank you for your time :)

推荐答案

尝试类似

<script type="text/javascript">
  function getVal(value)
  {

   alert(value);
  }
 </script>
 <input type="button" value="Add" onclick="getVal(this.value)">

这篇关于如何使用Javascript获取按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆