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

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

WBOY
Release: 2016-06-06 20:48:04
Original
1189 people have browsed it

在一个页面有很多个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>
Copy after login

后端页面:

<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>
Copy after login

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

<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>
Copy after login

然后 jQuery 这样处理

<code>$('.action').on('click', function() {
    var action = $(this).data('action');
    $.get(action, function(data) {
        // do something with data.
    });
});
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template