weixin_33691817 2019-08-06 10:47 采纳率: 0%
浏览 102

Ajax到servlet

Hi I'm having trouble on how can I send my array objects data from jsp to servlet,

var i = 1;
var a; 
$(document).ready(function(){

$('#add').click(function(){
    i++;
    $('#dynamic_field').append('<tr id="row'+i+'"><td><select name="txtProduct'+i+'" id="txtProduct'+i+'" class="form-control name_list" ><option value="">- Product -</option><%=obj.getProductOpt("")%></select></td><td><input type="text" name="txtQty'+i+'" id="txtQty'+i+'" class="form-control"/></td><td><input type="button" name="remove" value="X" id="'+i+'" class="btn btn-danger btn_remove"/> </td></tr>');
    var b = i - 1;
    $( "[id^="+b+"]" ).prop( "disabled", true );
    $( "[id^="+i+"]" ).prop( "disabled", false );
});
$(document).on('click', '.btn_remove', function(){
    i--;
    var button_id = $(this).attr("id");
    $('#row'+button_id+'').remove();
    var b = i;
    $( "[id^="+b+"]" ).prop( "disabled", false );
});

$('#addButton').click(function(){
    var data = [];
    var n = 0;
    var c = 0;
    for (a = 1; a <= i; a++) { 
        var txtProduct = document.getElementById("txtProduct"+a+"").value;
        var txtQty = document.getElementById("txtQty"+a+"").value;
        var val = {"pname": txtProduct, "qty": txtQty};
        data.push(val);

    }
    for(ii = 0; ii < a; ii++){
            var pname = [];
            var qty = [];
            pname[ii] = data[ii].pname;
            qty[ii] = data[ii].qty;
        }
});
});

The thing is I have put the values in the variable but my problem is I don't know how to send it to servlet via ajax or what, I tried watching tutorials about ajax but it does not make sense to me since they all have different approuches, I hope someone can help me, thanks

  • 写回答

1条回答 默认 最新

  • 程序go 2019-08-27 10:50
    关注

    It can done by using setAttributes in jsp: First you need to convert your array in the form of array list then execute the below code: request.getSession().setAttribute("Array List Name", Array List Name Object); ArrayList list=request.getParameter("Array List Name");

    评论

报告相同问题?