HREF链接ajax发布请求

时间:2016-03-13 08:42:47

标签: php ajax post request href

要拍摄,这是我的问题。我有一个脚本通过ajax发送Form POST请求,但我的问题是我想在A Href标签中执行。下面是我的FORM POST请求代码:

<script type="text/javascript">
$(document).ready(function(){
    $( ".searchForm" ).submit(function( event ) {
        event.preventDefault();

        var invoiceNo=$(".invoiceNo").val();
        var dataString = 'invoiceNo='+invoiceNo;

        $.ajax ({
            type: "POST",
            url: "<?php echo base_url(); ?>/billing/test",
            data: dataString,
            cache: false,
            success: function(html) {
                $(".particularList_output").html(html);
            } 
        });

    });
});
</script>

<form action="/" class="searchForm">
<input type="hidden" class="invoiceNo" value="thisIsMyValue">
<button id="xSmall" type="submit" class="btn btn-primary">Add Invoice</button>
</form>

1 个答案:

答案 0 :(得分:1)

做这样的事情:

<a href="<?php echo base_url();?>/billing/test" data-invoice="<?php echo $yourInvoiceNumber?>"> Add Invoice </a>

$('a').on('click', function(e){
    e.preventDefault();

    var $this = $(this);

    $.ajax({
        url: $this.data('href'),
        type: 'POST',
        data: 'invoiceNo='+$this.data('invoice'),
        cache: false,
        success: function(html) {
            $(".particularList_output").html(html);
        } 
    });
});