简体   繁体   English

如何使用getElemetByName获取单击的按钮名称?

[英]How do I get the clicked button name with getElemetByName?

I'm trying to get value of button which is clicked, or determine which button was clicked. 我试图获取被单击的按钮的值,或者确定被单击的按钮。

From code below, I'm getting all values but I just want the button that was clicked. 从下面的代码中,我获得了所有值,但我只想单击该按钮。

 <script type="text/javascript">
    function GetCheckedFruits () {
        var elements = document.getElementsByName ("fruit");
        for (var i=0; i < elements.length; i++) {
            if (elements[i].click) {
                alert ("The " + (i+1) + ". button is click");
            }
        }
    }
</script>

HTML: HTML:

<input id="Text1" name="fruit" type="button" value="apple" onclick="GetCheckedFruits ()" /><br />

<input id="Text1" name="fruit" type="button" value="banana" onclick="GetCheckedFruits ()" /><br />

<input id="Text1" name="fruit" type="button" value="blackberry" onclick="GetCheckedFruits ()" /><br />

Where am I going wrong? 我要去哪里错了?

change 更改

if (elements[i].click) {
     alert ("The " + (i+1) + ". button is click");
}

to

if (elements[i].type == 'button') {
    alert ("The " + elements[i].value + " button is click");
}

thats it. 而已。

EDIT 编辑

Try this 尝试这个

function GetCheckedFruits (clickedButton) { alert ("The " + clickedButton.value + ". button is click"); 函数GetCheckedFruits(clickedButton){警报(“ The” + clickedButton.value +“。button is click”); } } }}

and add the buttons like this 并添加这样的按钮

<input type="button" value="Button1" onclick="{GetCheckedFruits(this);}" /> <input type="button" value="Button2" onclick="{GetCheckedFruits(this);}" /> <input type="button" value="Button3" onclick="{GetCheckedFruits(this);}" /> <input type="button" value="Button1" onclick="{GetCheckedFruits(this);}" /> <input type="button" value="Button2" onclick="{GetCheckedFruits(this);}" /> <input type="button" value="Button3" onclick="{GetCheckedFruits(this);}" />

If you want to alert the message when the button is clicked, you would have to use an event like this: 如果要在单击按钮时提醒消息,则必须使用如下事件:

<script type="text/javascript">
    function GetCheckedFruits () {
        var elements = document.getElementsByName ("fruit");
        for (var i=0; i < elements.length; i++) {
            elements[i].onclick = function() {
                // You can put anything you want here
                // the variable `this` is the clicked button
                alert ("The " + this.value + " button is click");
            }
        }
    }
</script>

EDIT: 编辑:

In the case you are working with checkboxes and are trying to get which fruits where selected, this will work: 如果您正在使用复选框并尝试获取选中的水果,则可以使用:

<script type="text/javascript">
    function GetCheckedFruits () {
        var elements = document.getElementsByName ("fruit");
        for (var i=0; i < elements.length; i++) {
            if (elements[i].checked == 1) {
                alert ("The " + elements[i].value + " button is click");
            }
        }
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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