jQuery的ajaxpost

是否可以在单击元素时使用POST方法提交ID? 我认为jQuery有一个内置的AJAX扇形…

 

 $('#submitThisId').click(function() { $.post("postTo.php", { id: $(this).attr('id') }, function(response) { alert(response); }); }); 

这会将你的id发布到postTo.php,可以使用以下命令检索: $id = $_POST['id'];

无论postTo.php文件返回什么,都会向屏幕提醒(响应)。

编辑:要提交到同一页面并执行一些服务器端操作(数据库查询等),您只需要在页面顶部放置类似于以下代码的内容:

  

当“响应”(text,html等)返回到jQuery时,您可以使用简单的jQuery将其插入到页面中,并在必要时更改页面的外观。

 #submitThisID.post( 'http://example.com/', { 'foo': 'bar', }, function (data) { console.log( 'data: ' + data ); } ); 

这绝对是可能的: $.post()

但是,你的问题没有意义。 HTTPpost中的提交值采用名称/值对。 一个示例POST主体可能如下所示:

 Name: Jonathan Doe Age: 23 

元素ID是名称还是值? 另一个会是什么?

是的,请阅读: http : //api.jquery.com/jQuery.post/

  $('#submitThisID').click(function({ var id = $(this).attr('id'); $.ajax({ url:'/index.php/ajax/', type:'post', dataType:'json', data:{ id:id; }, success:function(data){ console.log('ajax success!'); } }); });