我正在尝试使用按钮进行 2 个单独的 AJAX 调用。我想要发生的是:当单击 Button1 时 ProductsTable 显示来自网络服务的数据;单击 Button2 时,OthersTable 会显示来自 web 服务的自己的数据。但是现在,当单击任一按钮时,什么都没有显示。我知道如果只有其中一个代码并且它没有包裹在 .click 函数周围,则该代码可以工作。
没有错误信息。ASP.NET 4.0,JQuery 1.4.4。不使用 ScriptManager。不使用更新面板。
下面的代码:
<script src="Scripts/jquery-1.4.4.js" type="text/javascript"></script>
<script src="Scripts/jquery.tmpl.js" type="text/javascript"></script>
<script id="ProductsTemplate" type="text/x-query-tmpl">
<tables id="ProductsTemplate">
<thead>
</thead>
<tbody>
{{each d}}
{{tmpl($value) '#ProductsRowTemplate'}}
{{/each}}
</tbody>
</tables>
</script>
<script id="ProductsRowTemplate" type="text/x-query-tmpl">
<tr>
<td>${title}</td>
<td>${size}</td>
<td>${price}</td>
</tr>
</script>
<script id="Products2Template" type="text/x-query-tmpl">
<tables id="Products2Template">
<thead>
</thead>
<tbody>
{{each d}}
{{tmpl($value) '#Products2RowTemplate'}}
{{/each}}
</tbody>
</tables>
</script>
<script id="Products2RowTemplate" type="text/x-query-tmpl">
<tr>
<td>${title}</td>
<td>${size}</td>
<td>${price}</td>
</tr>
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#Button1").click(function () {
$.ajax({
type: "POST",
url: "JSON-WebService.asmx/getProducts",
data: "{}",
contentType: "application/json; charset=UTF-8",
dataType: "json",
success: function (data) {
$('#ProductsTemplate').tmpl(data).appendTo('#ProductsTable');
alert("Products works");
},
failure: function (data) {
alert("Products didn't work");
}
});
});
$("#Button2").click(function () {
$.ajax({
type: "POST",
url: "JSON-WebService.asmx/getProducts",
data: "{}",
contentType: "application/json; charset=UTF-8",
dataType: "json",
success: function (data) {
$('#Products2Template').tmpl(data).appendTo('#OthersTable');
alert("Products2 works");
},
failure: function (data) {
alert("Products2 didn't work");
}
});
});
});
</script>
<title>Using JQuery</title>
<form id="form1" runat="server">
<div id="ProductsTable">
</div>
<div id="OthersTable">
</div>
<div>
<asp:Button ID="Button1" runat="server" Text="Products" />
<asp:Button ID="Button2" runat="server" Text="Products2" />
</div>
</form>