Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将sql查询变量添加到javascript中并进行计算?_Javascript_Html_Flask - Fatal编程技术网

如何将sql查询变量添加到javascript中并进行计算?

如何将sql查询变量添加到javascript中并进行计算?,javascript,html,flask,Javascript,Html,Flask,我正在制作一个病人系统,根据病人的生日计算他们的年龄,但是当我添加它时,我得到了错误 Javascript: <script> var dob = new Date("06/24/2008"); <!-- I wanted to add {{ patient.birth }} into the date part--> //calculate month difference from current date in time

我正在制作一个病人系统,根据病人的生日计算他们的年龄,但是当我添加它时,我得到了错误

Javascript:

<script>  
    var dob = new Date("06/24/2008");  <!-- I wanted to add {{ patient.birth }} into the date part-->
    //calculate month difference from current date in time  
    var month_diff = Date.now() - dob.getTime();  
      
    //convert the calculated difference in date format  
    var age_dt = new Date(month_diff);   
      
    //extract year from date      
    var year = age_dt.getUTCFullYear();  
      
    //now calculate the age of the user  
    var age = Math.abs(year - 1970);  
      
    //display the calculated age  
    document.write("Age of the date entered: " + age + " years");  
</script>  

var dob=新日期(“06/24/2008”);
//及时计算与当前日期的月差
var month_diff=Date.now()-dob.getTime();
//将计算出的差异转换为日期格式
var年龄=新日期(月差);
//从日期开始提取年份
var year=年龄(getUTCFullYear();
//现在计算用户的年龄
var年龄=数学abs(1970年);
//显示计算的年龄
文件。填写(“输入日期的年龄:“+年龄+”年”);
{{patient.birth}}是我想添加到脚本中的sql查询

var birthdate = $("#birth").val();

这取决于该值在jinja中的实现方式。 如果可以的话,让我知道哪一个适合你。 我过去用这两种方法都做过

注意:我把“病人”放在外面不是偶然的。这是故意的

如果他们正在更改此屏幕上的生日值。 然后你需要做一个onchange事件。 例如:

<head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script>

    <style>
        function makeChange() {
            var birthdate = $("birth").val();
        }
    </style>
</head>

<form method="POST" action="">
    <div onchange="makeChange()">
        {{ patient.birth }}
    </div>
</form>

函数makeChange(){
var birthdate=$(“出生”).val();
}
{{病人出生}

传递到日期构造函数的参数的格式应正确。 在您的案例中,patient.birth应该是字符串,就像您的示例中的“06/24/2008”

你不能这样说:

const date = new Date("3 1930");
它打印

无效日期


您正在发送post请求吗?或者您正在使用javascript客户端处理您的请求?不,我实际上是在使用现在的时间减去出生日期并获得用户的年龄
const date = new Date("3 1930");