编写清晰的jquery代码
我有一个带有多个按钮的html页面,这些按钮都执行jquery ajax代码。目前,我已经为每个按钮创建了一个单击事件处理程序,但是有更干净的方法吗编写清晰的jquery代码,jquery,Jquery,我有一个带有多个按钮的html页面,这些按钮都执行jquery ajax代码。目前,我已经为每个按钮创建了一个单击事件处理程序,但是有更干净的方法吗 $(function() { $("#btn_id_1").click(function() { $.ajax({ url: 'url1', type: 'POST', ... } } $("#btn_id_2").cli
$(function() {
$("#btn_id_1").click(function() {
$.ajax({
url: 'url1',
type: 'POST',
...
}
}
$("#btn_id_2").click(function() {
$.ajax({
url: 'url2',
type: 'POST',
...
}
}
$("#btn_id_3").click(function() {
$.ajax({
url: 'url2',
type: 'POST',
...
}
}
}
谢谢,
Dennis因为每个按钮的url都不同,所以请将每个按钮的数据属性如下所示
<input type="button" data-url="url1" id="btn_id_1">
或jQuery:
由于每个按钮的url都不同,所以请将每个按钮的数据属性如下所示
<input type="button" data-url="url1" id="btn_id_1">
或jQuery:
由于每个按钮的url都不同,所以请将每个按钮的数据属性如下所示
<input type="button" data-url="url1" id="btn_id_1">
或jQuery:
由于每个按钮的url都不同,所以请将每个按钮的数据属性如下所示
<input type="button" data-url="url1" id="btn_id_1">
或jQuery:
使用
属性选择器
适用于上下文
$("[id^='btn_id_']").click(function() {
var id = this.id; // get id of the current button
$.ajax({
url: 'url' + id.split("_")[1],
type: 'POST',
...
});
});
使用
属性选择器
适用于上下文
$("[id^='btn_id_']").click(function() {
var id = this.id; // get id of the current button
$.ajax({
url: 'url' + id.split("_")[1],
type: 'POST',
...
});
});
使用
属性选择器
适用于上下文
$("[id^='btn_id_']").click(function() {
var id = this.id; // get id of the current button
$.ajax({
url: 'url' + id.split("_")[1],
type: 'POST',
...
});
});
使用
属性选择器
适用于上下文
$("[id^='btn_id_']").click(function() {
var id = this.id; // get id of the current button
$.ajax({
url: 'url' + id.split("_")[1],
type: 'POST',
...
});
});
比如:
$("button").click(function (event) {
var href = event.srcElement.getAttribute("href");
$.ajax({
url: href,
...
});
});
比如:
$("button").click(function (event) {
var href = event.srcElement.getAttribute("href");
$.ajax({
url: href,
...
});
});
比如:
$("button").click(function (event) {
var href = event.srcElement.getAttribute("href");
$.ajax({
url: href,
...
});
});
比如:
$("button").click(function (event) {
var href = event.srcElement.getAttribute("href");
$.ajax({
url: href,
...
});
});
使用数据属性和非结构化javascript: Html示例:
<button class="my-class" data-ajax-url="url1">My button</button>
<button class="my-class" data-ajax-url="url2">My button</button>
<button class="my-class" data-ajax-url="url3">My button</button>
<button class="my-class" data-ajax-url="url3">My button</button>
使用数据属性和非结构化javascript: Html示例:
<button class="my-class" data-ajax-url="url1">My button</button>
<button class="my-class" data-ajax-url="url2">My button</button>
<button class="my-class" data-ajax-url="url3">My button</button>
<button class="my-class" data-ajax-url="url3">My button</button>
使用数据属性和非结构化javascript: Html示例:
<button class="my-class" data-ajax-url="url1">My button</button>
<button class="my-class" data-ajax-url="url2">My button</button>
<button class="my-class" data-ajax-url="url3">My button</button>
<button class="my-class" data-ajax-url="url3">My button</button>
使用数据属性和非结构化javascript: Html示例:
<button class="my-class" data-ajax-url="url1">My button</button>
<button class="my-class" data-ajax-url="url2">My button</button>
<button class="my-class" data-ajax-url="url3">My button</button>
<button class="my-class" data-ajax-url="url3">My button</button>
只有URL不同吗?只有URL不同吗?只有URL不同吗?只有URL不同吗?这是最好的答案。希望有人发布这个。很好的例子!谢谢。太好了,这就是我要找的!这是迄今为止最好的答案。希望有人发布这个。很好的例子!谢谢。太好了,这就是我要找的!这是迄今为止最好的答案。希望有人发布这个。很好的例子!谢谢。太好了,这就是我要找的!这是迄今为止最好的答案。希望有人发布这个。很好的例子!谢谢。太好了,这就是我要找的!