我希望能够从多个复选框发送真值或假值的JSON字典。 不幸的是,当我提交时,无论选中还是未选中,发送到Flask的值始终为“ on”,而不是true或false。 谁能帮忙吗?我将非常感谢。
这是我的HTML / JQuery代码
checkbox1.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="/static/images/neptune-icon2.png" type="image/png" sizes="16x16">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="/static/images/neptune-icon2.png" type="image/png" sizes="16x16">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<title>Check Box 1</title>
</head>
<body>
<center>
<input type="checkbox" id="checking-1" checked="false">
<input type="checkbox" id="checking-2" checked="false">
<br>
<input type="submit" name="submit" class="grab-checkboxes">
</center>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).on("click", ".grab-checkboxes", function (e) {
e.preventDefault();
// var info = $(this).parent().siblings('#checking-1').text();
var dateArray = [];
$('#checking-1').each(function () {
var check1 = $(this).val();
var check2 = $(this).parent().find('#checking-2').val();
dateArray.push({ 'checking1': check1, 'checking2': check2 });
});
console.log(dateArray);
console.log(dateArray.length);
console.log(Object.keys(dateArray));
var server = "http://127.0.0.1:5000";
var appdir = "/grab_checkboxes"
$.ajax({
type:"POST",
url: server + appdir,
data: JSON.stringify(dateArray),
dataType: "json",
contentType: "application/json;charset=UTF-8",
success: function(data){
console.log(data);
console.log("Checking...");
}
});
return false;
});
</script>
</body>
</html>
我的烧瓶代码。
from flask import Flask, render_template, jsonify, request
import json
app = Flask(__name__)
@app.route("/checkbox1", methods = ["GET", "POST"])
def checkbox1():
return render_template("checkbox1.html")
@app.route("/grab_checkboxes", methods = ["GET", "POST"])
def grab_checkboxes():
print("Grabbing 1 or more checkboxes that are true!")
data = request.get_json()
print(data)
return "1"
答案 0 :(得分:0)
您的jquery代码有问题。像这样更改您的jquery代码:
...
$('#checking-1').each(function () {
var check1 = $(this).is(':checked');
var check2 = $(this).parent().find('#checking-2').is(':checked');
dateArray.push({ 'checking1': check1, 'checking2': check2 });
});
...
通过这种方式可以正确发送每个复选框的状态。