来自外部文件的Javascript没有加载 [英] Javascript from external file not loading

查看:137
本文介绍了来自外部文件的Javascript没有加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Javascript问题,它让我一直在困扰我很长一段时间。我有一个名为search.js的外部文件,它与它加载到的.html文件位于同一文件夹中。



我在HTML中加载的一段代码加载javascript文件是:

 < script type =text / javascriptsrc =search.js>< /脚本> 

在我读过的所有网站中,我找不到代码。 / p>

另外,我知道JavaScript文件中的语法必须正确才能正常工作,因此,这里是我的Javascript代码 search.js

  $(document).ready(function(){
$('#searchForm')) .submit(function(){
var lookfor = $(#billSearched)。val();
alert(lookfor);
var a = $(this).attr(' (a);
alert(a);
a = a.replace(__ search_term __,lookfor);
alert(a);
window.location.href = a;
返回false;
});
});

我在另一个项目中运行了这段代码,它工作得很好,我所有的如果有任何其他原因导致Javascript未加载到我的页面中,请留言或留言。

预先致谢!

编辑

完整的html代码:

 < html> 
< head>
< title> Bills< / title>
< script type =text / javascriptsrc =/ jquery-1.7.1.min.js>< / script>
< script type =text / javascriptsrc =/ search.js>< / script>
< link rel =stylesheethref =style.css>
< / head>
< body bgcolor =#0066CCcolor =FFFFFF>
< table>
< tr>
< td style =color:white>< b>产品< / b>< / td>
< td style =color:white> Price< / td>
< form method =POSTid =searchFormaction ={%url ps.views.search searchTerm ='__ search_term__'%}>
{%csrf_token%}
< td>< input type =textid =billSearched>< / td>
< td>< input type =submitvalue =Search>< / td>
< / form>
< / tr>
{%产品中的x%}
< tr>
< td style =color:white> {{x.name}}< / td>
< td style =color:white> {{x.price}}< / td>
< / tr>
{%endfor%}
< / tr>
< / table>
< / body>


解决方案 $ b

  • 在settings.py文件中将'/ static /'文件夹添加到STATIC_URL和STATICFILES_DIRS li>
  • 按以下顺序使用以下命令导入.js文件(search.js和jQuery源文件):

     < script type =text / javascriptsrc =/ static / jquery-1.7.1.min.js>< / script> 
    < script type =text / javascriptsrc =/ static / search.js>< / script>


  • 然后制作相应的文件结构,将静态文件放入项目的根目录。




    Psroot


  • psApp

  • 静态

  • 模板

  • 和其他文件(settings.py,urls.py,manage.py等)


I have a problem with Javascript that keeps bugging me for quite a while now. I have an external file called search.js which is in the same folder as the .html file it's loaded into.

The piece of code I use in th HTML to load the javascript file is:

<script type="text/javascript" src="search.js"></script>

From all the websites I've read I can't find an issue here with the code.

Also, I know that the syntax in the javascript file has to be correct in order for it to work, so here is my Javascript code from search.js:

$(document).ready(function(){
    $('#searchForm').submit(function(){
        var lookfor = $("#billSearched").val();
        alert(lookfor);
        var a = $(this).attr('action');
        alert(a);
        a = a.replace("__search_term__",lookfor);
        alert(a);
        window.location.href = a;
        return false;
    });
});

I've runned this code in another project and it has worked just fine, all that I've changed was the names of the fields, i.e. billSearched.

If there are any other reasons for why Javascript doesn't load in my page please leave a message or a comment.

Thanks in advance!

EDIT

Full html code:

<html>
<head>
    <title>Bills</title>
    <script type="text/javascript" src="/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="/search.js"></script>
    <link rel="stylesheet" href="style.css"> 
</head>
<body bgcolor="#0066CC" color="FFFFFF">
    <table>
        <tr>
            <td style="color:white"><b>Products</b></td>
            <td style="color:white">Price</td>
            <form method="POST" id="searchForm"  action="{% url ps.views.search searchTerm='__search_term__' %}">
                {% csrf_token %}
                <td><input type="text" id="billSearched"></td>
                <td><input type="submit" value="Search"></td>
            </form>
        </tr>
        {% for x in products %}
        <tr>
        <td style="color:white">{{ x.name }}</td>
        <td style="color:white">{{ x.price }}</td>
        </tr>
        {% endfor %}
    </tr>
</table>
</body>

解决方案

I had to make a mix of your answers in order to fix the problem but eventually it was fixed. I had to make the following modifications:

  • Add the '/static/' folder to STATIC_URL and STATICFILES_DIRS in the settings.py file
  • Import the .js files (search.js and the jQuery source) with the following commands, in this order:

    <script type="text/javascript" src="/static/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="/static/search.js"></script>
    

  • And make the according file structure, placing the "static" file in the project's root directory.

    i.e. Psroot:

  • psApp
  • static
  • templates
  • and the rest of the files (settings.py, urls.py, manage.py, etc.)

这篇关于来自外部文件的Javascript没有加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆