Sending post data using html a tag

被刻印的时光 ゝ 提交于 2019-12-13 08:08:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!