2021年5月4日星期二

[Flask][Ajax] How to send and get multiple checkbox values

I'm trying to submit multiple checkbox values using AJAX and then add checked items to div element. Because a number of checkbox varies depending on cases, I don't know a way to send and get such a multiple data. In HTML code below, the length of the list person_name changes depending on users.

HTML:

    <a href=# id="add">Add</a>                                             <div id="names">        Your checked names are:            </div>  

Javascript:

$('a#add').bind('click',function(){  $.ajax({      url: "/regist",      type: "GET",      contentType: 'application/json;charset=UTF-8',      data: { /* I can't find a way */},      dataType:"json",      success: function (data) {      var div = document.createElement('div');      document.getElementById('wrapper').appendChild(div);      }   });  })  

Python:

@app.route("/")  def index():    @app.route("/regist")  def register():      cknames = request.form.getlist('check')  return cknames %??  

In addition I don't want to redirect the page, want to just add div element. Appreciate any helps!

https://stackoverflow.com/questions/67394290/flaskajax-how-to-send-and-get-multiple-checkbox-values May 05, 2021 at 10:07AM

没有评论:

发表评论