Simple ajax form using javascript no jQuery

前端 未结 9 1057
终归单人心
终归单人心 2020-11-28 11:46

I\'m working with a form for which the mark-up I can\'t change & can\'t use jQuery. Currently the form post the results to a new window. Is it possible to change this to

9条回答
  •  借酒劲吻你
    2020-11-28 12:41

    The strategy is to serialise the form and send the data using XHR, then do what you want with the response. There is a good set of utilities and help at Matt Krus's Ajax Toolbox and related Javascript Toolbox.

    If you are just serialising the form posted, then the following will do the trick. It can easily be extended to include other form control types:

    var serialiseForm = (function() {
    
      // Checkboxes that have already been dealt with
      var cbNames;
    
      // Return the value of a checkbox group if any are checked
      // Otherwise return empty string
      function getCheckboxValue(cb) {
        var buttons = cb.form[cb.name];
        if (buttons.length) {
          for (var i=0, iLen=buttons.length; i

提交回复
热议问题