0

我的视图中有以下类型的按钮。这些按钮是通过服务器端代码动态生成的。所以我的问题是我需要编写通用的 jquery 点击事件来执行该按钮的相关任务。(这里我有所有按钮的通用方法作为服务器代码)。那么如何检测特定按钮以编写我的通用服务器调用?

注意:所有按钮的共同部分是预付费包

 <button id="pre-paid-package-12-day-camp" class="actionButton pre-paid-package-12-day-camp " name="key" value="pre-paid-package-12-day-camp" type="submit">
      Apply 12-Day Camp package
   </button>
<button id="pre-paid-package-20-day-camp" class="actionButton pre-paid-package-20-day-camp " name="key" value="pre-paid-package-20-day-camp" type="submit">
      Apply 20-Day Camp package
   </button>
4

2 回答 2

3

您可以执行以下操作

html

    <button id="pre-paid-package-12-day-camp" class="actionButton pre-paid-package-12-day-camp " name="key" value="pre-paid-package-12-day-camp" type="submit">Apply 12-Day Camp package</button>
    <button id="pre-paid-package-20-day-camp" class="actionButton pre-paid-package-20-day-camp " name="key" value="pre-paid-package-20-day-camp" type="submit">Apply 20-Day Camp package</button>

$(document).on('click', '[id^="pre-paid-package"]', function() {
    alert('clicked');
});

提琴手

编辑 根据安东的建议,我已经更新了我的答案

于 2013-09-27T08:09:15.020 回答
1

您可以使用.on()jquery 属性选择器

$("button[id^='pre-paid-package']").on('click', function(){
   // code here
});

^是从

于 2013-09-27T08:10:08.783 回答