Jquery getJSON不使用输入按钮
如果我有Jquery getJSON不使用输入按钮,jquery,jquery-selectors,Jquery,Jquery Selectors,如果我有a hrefgetJSON按预期工作正常(获取警报消息框),但如果我使用input按钮,则它不工作(没有警报消息),下面是我使用a href和input按钮的示例,我打开firebug,没有看到任何数据响应 工作 <a href="'http://host/Myservice.svc/GetCustomerBy?GetCustomerBy?GetCustomerBy=?">GetCustomerBy</a> $(function () {
a href
getJSON按预期工作正常(获取警报消息框),但如果我使用input按钮,则它不工作(没有警报消息),下面是我使用a href
和input按钮的示例,我打开firebug,没有看到任何数据响应
工作
<a href="'http://host/Myservice.svc/GetCustomerBy?GetCustomerBy?GetCustomerBy=?">GetCustomerBy</a>
$(function () {
$('a').click(function () {
$.getJSON(this.href, { id: '2' }, function (customer) {
alert(customer.Name);
alert(customer.Address);
});
return false;
});
});
<input type="button" id="driver" value="Load Data" />
$("#driver").click(function (event) {
$.getJSON('http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?', { id: '2' }, function (customer) {
alert(customer.Address);
alert(customer.Name);
});
});
$(函数(){
$('a')。单击(函数(){
$.getJSON(this.href,{id:'2'},函数(customer){
警报(客户名称);
警报(客户地址);
});
返回false;
});
});
不工作
<a href="'http://host/Myservice.svc/GetCustomerBy?GetCustomerBy?GetCustomerBy=?">GetCustomerBy</a>
$(function () {
$('a').click(function () {
$.getJSON(this.href, { id: '2' }, function (customer) {
alert(customer.Name);
alert(customer.Address);
});
return false;
});
});
<input type="button" id="driver" value="Load Data" />
$("#driver").click(function (event) {
$.getJSON('http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?', { id: '2' }, function (customer) {
alert(customer.Address);
alert(customer.Name);
});
});
$(“#驱动程序”)。单击(函数(事件){
$.getJSON('http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?“,{id:'2'},函数(客户){
警报(客户地址);
警报(客户名称);
});
});
试试这个:
$(function() {
$('#driver').click(function() {
$.getJSON('http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?', { id: '2' }, function (customer) {
alert(customer.Address);
alert(customer.Name);
});
});
});
请注意,调用被包装在$(document).ready中,并且不需要返回false
。也不需要对匿名回调使用事件
参数
此外,您的href地址与您在按钮中使用的地址不同。在href中,您有:
http://host/Myservice.svc/GetCustomerBy?GetCustomerBy?GetCustomerBy=?
http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?
而在按钮中,您有:
http://host/Myservice.svc/GetCustomerBy?GetCustomerBy?GetCustomerBy=?
http://host/Myservice.svc/GetCustomerBy?GetCustomerBy=?
这是不一样的。因此,无论这个地址是什么,都要确保使用正确的地址。在这种情况下会对您有所帮助。您的URL看起来不太正确——太多了?而且GetCustomerBy
似乎在重复。难道不是:
<input type="button" id="driver" value="Load Data" />
$("#driver").click(function (event) {
$.getJSON('http://host/Myservice.svc/GetCustomerBy', { id: '2' }, function (customer) {
alert(customer.Address);
alert(customer.Name);
});
});
$(“#驱动程序”)。单击(函数(事件){
$.getJSON('http://host/Myservice.svc/GetCustomerBy“,{id:'2'},函数(客户){
警报(客户地址);
警报(客户名称);
});
});
这将导致URL看起来像http://host/Myservice.svc/GetCustomerBy?id=2