如何使这个jquery函数可重用?
海这是我的 jquery 函数,
function getRecordspage(curPage, pagSize) {
$.ajax({
type: "POST",
url: "Default.aspx/GetRecords",
data: "{'currentPage':" + curPage + ",'pagesize':" + pagSize + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(jsonObj) {
var strarr = jsonObj.d.split('##');
var jsob = jQuery.parseJSON(strarr[0]);
$.each(jsob.Table, function(i, employee) {
$('<div class="resultsdiv"><br /><span class="resultName">' + employee.Emp_Name + '</span><span class="resultfields" style="padding-left:100px;">Category :</span> <span class="resultfieldvalues">' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" class="resultfields">Salary Basis :</span> <span class="resultfieldvalues">' + employee.SalaryBasis + '</span><span class="resultfields" style="padding-left:25px;">Salary :</span> <span class="resultfieldvalues">' + employee.FixedSalary + '</span><span style="font-size:110%;font-weight:bolder;padding-left:25px;">Address :</span> <span class="resultfieldvalues">' + employee.Address + '</span></div>').appendTo('#ResultsDiv');
});
$(".resultsdiv:even").addClass("resultseven");
$(".resultsdiv").hover(function() {
$(this).addClass("resultshover");
}, function() {
$(this).removeClass("resultshover");
});
}
});
}
在我的页面中我有这个 jquery 脚本,
$("#lnkbtn0").click(function() {
getRecordspage(1, 5)
});
并且我的页面有
如何让其他链接按钮使用 diff curPage
参数调用此函数...
Hai this is my jquery function,
function getRecordspage(curPage, pagSize) {
$.ajax({
type: "POST",
url: "Default.aspx/GetRecords",
data: "{'currentPage':" + curPage + ",'pagesize':" + pagSize + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(jsonObj) {
var strarr = jsonObj.d.split('##');
var jsob = jQuery.parseJSON(strarr[0]);
$.each(jsob.Table, function(i, employee) {
$('<div class="resultsdiv"><br /><span class="resultName">' + employee.Emp_Name + '</span><span class="resultfields" style="padding-left:100px;">Category :</span> <span class="resultfieldvalues">' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" class="resultfields">Salary Basis :</span> <span class="resultfieldvalues">' + employee.SalaryBasis + '</span><span class="resultfields" style="padding-left:25px;">Salary :</span> <span class="resultfieldvalues">' + employee.FixedSalary + '</span><span style="font-size:110%;font-weight:bolder;padding-left:25px;">Address :</span> <span class="resultfieldvalues">' + employee.Address + '</span></div>').appendTo('#ResultsDiv');
});
$(".resultsdiv:even").addClass("resultseven");
$(".resultsdiv").hover(function() {
$(this).addClass("resultshover");
}, function() {
$(this).removeClass("resultshover");
});
}
});
}
In my page i have this jquery script,
$("#lnkbtn0").click(function() {
getRecordspage(1, 5)
});
And my page has
<a ID="lnkbtn0" class="page-numbers">1</a>
<a ID="lnkbtn1" class="page-numbers">2</a>
<a ID="lnkbtn2" class="page-numbers">3</a>
<a ID="lnkbtn3" class="page-numbers">4</a>
How to make other link buttons to call this function with diff curPage
parameter...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的意思是您想要应用 ID 以 linkbtn 开头的链接文本作为第一个参数:
请参阅 http://api.jquery.com/attribute-starts-with-selector/
或者,您可以只使用类选择器:
I think you mean you want to apply the text of links with ID starting with linkbtn as the first parameter:
See http://api.jquery.com/attribute-starts-with-selector/
Alternatively, you could just use a class selector:
如果您不介意使用普通旧式 javascript,
请确保在
标记上使用
preventDefault
。否则,如果你真的想使用 JQuery。
更新 使用建议的类选择器(如 karim79)。
If you don't mind using Plain old javascript
Make sure you
preventDefault
on the<a>
tags.Else, if you really want to go JQuery.
UPDATE Use class selectors (as karim79) suggested.