javascript - send input value via ajax without using val() function -
my use-case : have several input :
<label class="minilabel">field1</label><input type="text"> ... <label class="minilabel">fieldn </label><input type="text"> i have value of each input , send via ajax (a post request). set id each input retrieve through $("#id1").val() .. $("#idn").val() , such intricate. there alternative way these values?
$('form').serialize(); ...is friend. turns data in form query string pass in data attribute.
i ideally wrap elements in form (unless these dynamically created) can reference "action" attribute when sending ajax post request , page remains html4/5 compliant. this:
var $form = $('form'); $.ajax({ "type": "post", "url": $form.attr('action'), "data": $form.serialize(), "success": function (r) { } }); on other side of can serialise jquery object of collected input elements simple this:
$(":input").serialize(); (thanks dziamid info)
Comments
Post a Comment