问题
I would like to send POST data using an html tag. I know that there is no way doing this unless i script. However i tried , but couldn't get it to work.
<a class="test" onClick="assign()"><img src='<?php echo $accounts[$i]['Account']['image']; ?>' /> <?php echo $accounts[$i]['Account']['screen_name']; ?></a>
I tried using this:
function assign(){
$.post("/Accounts/index",
{ data: "test"
});
}
and i also tried this :
$(document).ready(function(){
$(".test").click(function(){
$.post("/accounts/index",
{ data: "test"
});
});
});
回答1:
Try this :
$(".test").click(function () {
$.ajax({ url: 'http://.....your path...../accounts/index',
data: {test:1},
type: 'post',
success: function(output) {
//your code
}
});
});
回答2:
It may be helpful to prevent the anchor's default click action by returning false like so:
<a class="test" onClick="assign(); return false;"> ...
or like so:
...
$(".test").click(function(){
...
return false;
});
来源:https://stackoverflow.com/questions/11686905/sending-post-data-using-html-a-tag