通过提交按钮获取值时出现Jquery问题

时间:2011-08-01 05:15:03

标签: php jquery codeigniter jquery-selectors

JQUERY代码

  $('#cartSubmit').click(function() {


        var product_name  = encodeURIComponent($('#product_name').val());
        var barcode  = encodeURIComponent($('#barcode').val());
        var Quantity  = encodeURIComponent($('#Quantity').val());


            var postData    =   "product_name="+product_name+"&barcode="+barcode+"&Quantity"+Quantity;
            $.ajax
                ({
                   type: 'POST',
                    url: 'http://localhost/example/index.php/cart/cartoutput',                      
                    data: postData,
                    success: function(html) {
                        //alert(html);
                        $('#cartDisplay').html(html);   

                        }
            });

        return false;
    }); 

查看代码

     <tr><td  align="center" > <input type="submit"  value="SUBMIT" id="cartSubmit"/></td> </tr>

我无法显示输出。代码中有什么问题

3 个答案:

答案 0 :(得分:0)

出于某种原因,我也很难让html()过去工作。尝试:

document.getElementById("#cartDisplay").innerHTML = html;

答案 1 :(得分:0)

购物车是由JS还是ajax添加的?

DOM中的cartSubmit内是cartDisplay吗?

尝试更改:

$('#cartSubmit').click(function () {

要:

$('#cartSubmit').live ('click', function () {

答案 2 :(得分:0)

尝试以下操作,看看它是否有效,我也制作了错误处理程序

$('#cartSubmit').click(function(e) {

   e.preventDefault(); //prevent the default behaviour of the submit

   $.ajax
    ({
      type: 'POST',
      url: 'http://localhost/example/index.php/cart/cartoutput',                      
      data: {product_name:product_name,barcode:barcode,Quantity:Quantity},
      dataType:'html', // i assumed you are returning html
      success: function(html) {
            alert("success");
           $('#cartDisplay').html(html);   
           },
      error:function(jxhr){ // make an error handler 
           alert(jxhr.responseText);
           }
        });       
});