Home >Backend Development >PHP Tutorial >javascript - 在一个页面有很多个button,怎么用 js控制button响应后端的php不同的action

javascript - 在一个页面有很多个button,怎么用 js控制button响应后端的php不同的action

WBOY
WBOYOriginal
2016-06-06 20:48:041170browse

在一个页面有很多个button,怎么用 js控制button响应后端的php不同的action

回复内容:

在一个页面有很多个button,怎么用 js控制button响应后端的php不同的action

前端页面:

<code><button onclick="request('do.php?act=what')">What</button>
<button onclick="request('do.php?act=hell')">Hell</button>
<button onclick="request('do.php?act=you')">You</button>
<button onclick="request('do.php?act=ask')">ask</button>
</code>

后端页面:

<code><?php switch($_GET['act']) {
        case 'what':
            /*
             * do sth from what
             */
        break;
        case 'hell':
            /*
             * do sth from hell
             */
        break;
        case 'you':
            /*
             * do sth from you
             */
        break;
        case 'ask':
            /*
             * do sth from ask
             */
        break;
    }
?>
</code>

趁早结了这个没意义的问题吧,一个问题翻来覆去的问好几遍..有意思么!

<code><button class="action" data-action="action1.php">Action1</button>
<button class="action" data-action="action2.php">Action2</button>
<button class="action" data-action="action3.php">Action3</button>
</code>

然后 jQuery 这样处理

<code>$('.action').on('click', function() {
    var action = $(this).data('action');
    $.get(action, function(data) {
        // do something with data.
    });
});
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn