Skip to content

Instantly share code, notes, and snippets.

@u1and0
Last active November 15, 2018 06:18
Show Gist options
  • Select an option

  • Save u1and0/658baeecf658cd05eea4d69e533f957e to your computer and use it in GitHub Desktop.

Select an option

Save u1and0/658baeecf658cd05eea4d69e533f957e to your computer and use it in GitHub Desktop.
Flask + jQueryでテーブルの入力フォームに入力があるたびに値を取得 ref: https://qiita.com/u1and0/items/6dcfdf60b02eba207064
$(function ($){
// テーブルの入力フォームにインプットした際に発動
$('td input').on('input',function () {
// name属性と入力フォームの値を取得
var inp = {name: $(this).attr('name'),
value: $(this).val()};
console.log(inp); // 入力をjavascriptでコンソールに表示
inp = JSON.stringify(inp);
$.post({
url: '{{ url_for('complete') }}', // server.py complete()に渡す
data: inp,
dataType: 'json',
contentType: 'application/json;charset=UTF-8',
}).done(function(data){
console.log(data); // 入力をserver.pyを通して表示
}).fail(function(){
console.log('fail');
});
});
});
from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify
from flask_bootstrap import Bootstrap
(略)
@app.route('/complete', methods=['POST'])
def complete():
json_data = request.json
return jsonify({'value': json_data})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment