duanke1286 2016-10-15 05:43
浏览 382
已采纳

Javascript隐藏/显示按钮[关闭]

I'm trying to hide/show a button on my form depending on whether the credit card validation returns true. So they can click checkout if it is true, and otherwise they cannot.

<script>
    $(function test() {

        $('#cardNo').validateCreditCard(function (result) {

            $('.log').html('Card type: ' + (result.card_type == null ? '-' : result.card_type.name)
                    + '<br>Valid: ' + result.valid
                    + '<br>Length valid: ' + result.length_valid
                    + '<br>Luhn valid: ' + result.luhn_valid);
            console.log(result.valid);
            console.log(result.length_valid);
            console.log(result.luhn_valid);
            if (result.valid == true && result.length_valid == true && result.luhn_valid == true) {
                console.log('we have entered the if statement');
                document.getElementById("checkoutButton").style.dislay = 'block';
            }
            else {
                console.log('we are not in the if statement');
                document.getElementById("checkoutButton").style.display = 'none';
            }
        });
    });
</script>

I have verified that when I enter a correct credit card number, we do go into the if statement. The console will read 'we have entered the if statement'. However, the checkout button will not become visible on the page.

I am wondering if it is even possible to do something like this in javascript? Thanks for the help!

  • 写回答

3条回答 默认 最新

  • dongshangan2074 2016-10-15 05:52
    关注

    just replace your code with this one:

                if (result.valid == true && result.length_valid == true && result.luhn_valid == true) {
                    console.log('we have entered the if statement');
                    document.getElementById("checkoutButton").style.display = 'block';
                }
                else {
                    console.log('we are not in the if statement');
                    document.getElementById("checkoutButton").style.display = 'none';
                }
    

    That's it, Enjoy coding :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao