单独的js文件中的jQuery在Flask中不起作用

时间:2018-11-10 18:40:51

标签: python jquery flask

我有一些jquery代码,当在行内使用时效果很好。如我所愿,我的jquery文件位于下面所示的目录中。

static/js/jqueryfunctions.js

我知道您需要在标记内调用url_for函数,以便解析正确的url。引用上一个问题here,但我无法使其与我的代码一起使用。

我的jqueryfunctions.js文件仅包含:

$(function() {
$('p select[name=school]').change(function(e){
if ($('p select[name=school]').val() == 'not-listed'){
  $('#new-school').show();
}else{
  $('#new-school').hide();
}
});

});

它所要做的就是,如果单击下拉列表的特定选项,它将使输入框出现在我称为register.html的文件中,该文件扩展了另一个名为index.html的文件

为简单起见,我的index.html是

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
{{%block body%}{%endblock %}
<script src="{{ url_for('static', filename='js/jquery.js') }}"></script>
<script src="{{ url_for('static', filename='js/jqueryfunctions.js') }}"></script> 
</body>

</html>

给我的印象是,我应该将url_for函数放在index.html文件的body标签底部。我也尝试过将其放在head标签中,但这也不起作用。为简单起见,我的register.html是

{% extends "index.html" %}
{% block body %}
<select  name="school" class="form-control" required>
                            <option value="" disabled selected>Select your school</option>
                            {% for schools in schoolarray %}
                            <option value ="{{schools['school_name']}}">{{schools['school_name']}}</option>
                            {% endfor %}
                            <option value="not-listed">My school is not listed</option>
                        </select>
                    </div>
                    <div id="new-school" style="display:none; margin-bottom:25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-new-window"></i></span>
<input type="text" class="form-control" name="school-name" placeholder="Please enter in the name of your school" required>

</div>

</p>

我不明白我在做什么错,这是因为它不很简单。我觉得这是一件非常小的事情,需要更改以使其起作用。

0 个答案:

没有答案
相关问题