Passing an array of values in an ASP.NET jQuery AJAX POST

前端 未结 3 1116
执念已碎
执念已碎 2020-12-10 23:27

I have a ListBox on my page, and I\'d like to make an AJAX post containing all the selected items. Here\'s my code:

$(\'#btnSubmit\').click(function() {
             


        
3条回答
  •  北海茫月
    2020-12-10 23:59

    Is the select element inside a form tag? You can serialize the entire form and send it through easily.

    var formdata = $('#formId').serialize();
    

    Then in your ajax call

    data: formdata,
    

    --

    Recently I dealt with a very similar issue. I am also using a C# Web Service, but the data I am dealing with is completely dynamic. I had to find a way to collect a variable length array and pass it as a single JSON string (along with some other fixed variables). I used the JSON2 library from http://json.org and collected the data by putting a certain class on all desired input fields, and using the title attribute as a key.

    var formdata = {};
    $(".formInputField").each(function() {
        formdata[$(this).attr('title')] = $(this).val();
    });
    
    var mydata = JSON.stringify({ 'formdata': JSON.stringify(formdata), 'othervar' : otherVal1, 'othervar2' : otherVal2, 'othervar3' : otherVal3 });
    

    Then I passed mydata as above.

提交回复
热议问题