使用jQuery检查多个提交按钮的问题 [英] Issue with checking for multiple submit buttons using jQuery

查看:53
本文介绍了使用jQuery检查多个提交按钮的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery 1.7.2检查是否按下了四个提交按钮,如果单击了三个按钮中的任何一个,我想显示一个带有按钮值的警报框.这是html代码:

I'm trying to check which of four submit buttons was pressed, using jQuery 1.7.2 If any of three of them are clicked, I want to show an alert box with the value of the button. Here is the html code:

<body>
    <table>
        <tr>
            <td>
                <input type="submit" id="cancel" value="Cancel">
            </td>
        </tr>

        <tr>
            <td>
                <input type="submit" id="find" value="Find">
            </td>
        </tr>

        <tr>
            <td>
                <input type="submit" id="save" value="Save">
            </td>
        </tr>

        <tr>
            <td>
                <input type="submit" id="update" value="Update">
            </td>
        </tr>
    </table>
</body>

这是jQuery函数代码:

Here is the jQuery function code:

<script>
        $(function(){
            $(':submit').click(function(){
                var value = $(this).val();
                //alert(value);

                if($(this).val() == "Save" || "Find" || "Clear") {
                //if($(this).val() == "Update") {
                    alert('The value is: ' + value);
                }
            });
        });
    </script>

如果我单击三个按钮中的任何一个,我都会得到显示的值,但是如果我单击更新按钮,它也会显示出来.我以为我的if块可以阻止这种情况的发生?

If I click on any of the three buttons I get the value displayed, but if I then click on the Update button it is also displayed. I thought my if block would prevent that from happening?

如果我注释掉该行并改用此代码:

If I comment out that line and use this code instead:

if($(this).val() == "Update") {
                        alert('The value is: ' + value);
                    }

然后,只有当我单击更新"按钮时,警报才会触发.当我检查多个提交按钮时,这里的代码有什么问题?

Then only when I click on the Update button will the alert fire. What is wrong with the code here when I'm checking for multiple submit buttons?

推荐答案

您的if语句不正确:

if( $(this).val() == "Save" || "Find" || "Clear" )

尽管通常假定操作员是这样工作的,但这不是它的工作方式.如果要测试值是否等于以下字符串之一,则可能应该使用$.inArray代替:

Though the operator is commonly assumed to work like this, this is not how it works. If you wanted to test whether the value is equal to one of these strings, you should probably use $.inArray instead:

if ( $.inArray( $(this).val(), [ "Save", "Find", "Clear" ] ) > -1 );

额外功劳

作为额外的功劳,||运算符对两个表达式求值(一个表达式在其左侧,一个表达式在其右侧),并且如果两个表达式之一为true. >真实.

Extra Credit

As a bit of extra-credit, the || operator evaluates two expressions (one to the left of it, and one to the right of it) and evaluates to true if either of the two expressions is truthy.

if ( true || false ) // Evalutes to true, since one of the two was true

在尝试时,您想查看该值是否等于这些字符串中的任何一个.如果要使用||,则必须创建多个比较:

In your attempt, you wanted to see if the value was equal to any of these strings. If you wanted to use ||, you would have to create several comparisons:

var myVal = $(this).val();
if ( ( myVal == "Save" ) || ( myVal == "Find" ) || ( myVal == "Clear" ) )

如果满足任何条件,则此条件的评估结果为true.您会看到它很快就会变得很冗长.

This condition will evaluate to true if any of the conditions are met. You can see how this will quickly become rather verbose.

这篇关于使用jQuery检查多个提交按钮的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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