2013-09-25 70 views
1

我的jquery工作正常,但是當我嘗試使它外部它似乎並不工作。 我有兩個腳本存儲在:include('./ includes/header.html'); 而且我也將它們存儲在文件夾的根目錄下。JavaScript的/ jquery文件未加載

如果有人能幫上忙。

我有header.html中

<!DOCTYPE html> 
    <head> 
    <title> <?php $page_title ?> </title> 

<link rel="stylesheet" type="text/css" href="mystyle.css"> 

<script src="formValidation.js"></script> 
<script src="jquery-1.10.2.js"></script> 



</head> 
<body> 
<div id="wrapper"> 

    <div id="header"> 
     <h1>Films.com</h1> 

    </div> 

    <div id="content"> 

     <div id="nav"> 

      <ul> 
       <li><a href="login.php">Login</a></li> 
       <li><a href="index.php">Home</a></li> 
       <li><a href="register.php">Register</a></li> 
       <li><a href="film.php">Film</a></li> 
       <li><a href="add_film.php">Add a Film</a></li> 


      </ul> 

     </div> 

內舉行當去add_film.php Jquery的,通常會滑下我的表單兩個環節,現在它不會滑下。

<?php 

    include('./includes/header.html'); 




echo "<h1>Add A film</h1>"; 

if(isset($_POST['submitted'])){ 

$errors = array(); // Initialize erroy array. 

// Check for title. 
if (empty($_POST['movie_title'])){ 
    $errors[] = "You forgot to enter a title."; 
} else { 
    $mn = (trim($_POST['movie_title'])); 
} 
// Check for leading actor 
if (empty($_POST['leading_actor'])){ 
    $errors[] = "You forgot to enter the leading actor."; 
} else { 
    $la = (trim($_POST['leading_actor'])); 
} 
// Check for a rating 
if (empty($_POST['rating'])){ 
    $errors[] = "Please select a rating."; 
} else { 
    $rating = ($_POST['rating']); 
} 
// Check for a review 
if (empty($_POST['review'])){ 
    $errors[] = "Please write a review"; 
} else { 
    $review = (trim($_POST['review'])); 
} 


if (empty($errors)) { // If no errors were found. 

    require_once('./includes/Mysql_connect.php'); 

    // Make the insert query. 
    $query = "INSERT INTO films (movie_title, actor, rating) 
    Values ('$mn', '$la', '$rating')"; 

    $result = mysql_query($query); 

    $id = mysql_insert_id(); 

    $query = "INSERT INTO reviewed (review, movie_id) 
    values ('$review', '$id')"; 

    $result = mysql_query($query); 



    //Report errors. 
} else { 
    foreach ($errors as $msg){ 
     echo " - $msg <br/> "; 
    } 
} 

} 

?>

<html> 


<form action="add_film.php" method="post" id="add_film"> 

    <fieldset> 

    <label for="title">Movie Title</label> 
    <input type="text" name="movie_title" id="movie_title" /> 

    <br/> 
    <br/> 

    <label for="actor">Leading Actor</label> 
    <input type="text" name="leading_actor" id="leading_name" /> 

    <br/> 
    <br/>  


    <label for="rating">Rating</label> 
    <select id="rating" name="rating"/> 
     <option selected="selected" value=0 disabled="disabled">Select a      Rating</option> 
     <option value="Terrible">Terrible</option> 
     <option value="Fair">Fair</option> 
     <option value="Ok">Ok</option> 
     <option value="Good">Good</option> 
     <option value="Excellent">Excellent</option> 
    </select> 

    <br/> 
    <br/> 


    <label for="review">Your Review</label> 
    <br/> 
    <textarea name="review" id="review" rows="15" cols="60"></textarea> 

    <br/> 
    <br/> 

    <input type="submit" name="submit" id="submit" value="submit" /> 
    <input type="hidden" name="submitted" value="TRUE" /> 

    </fieldset> 
</form> 







    <br/> 
</html> 







<?php 

include('./includes/footer.html'); 

    ?> 

這裏是我的jQuery的。

$(document).ready(function(){ 


    $('#add_film').slideDown(800); 

    // Focus on first form field. 
    $("input:text:visible:first").focus(); 


    $('#add_film').submit(function (e) { 

    var error = false; 

    // No value for movie_title 
    if ($('#movie_title').val() == "") { 
     alert("No Film"); 
     error = true; 
    } 

    // No Value for actor 
    if ($('#leading_name').val() == "") { 
     alert("No actor"); 
     error = true; 
    } 

    // No value for rating 
    if ($('#rating').val() == null) { 
     alert("No Rating"); 
     error = true; 
    } 

    //No value for review 
    if ($('#review').val() == "") { 
     alert("No review"); 
     error = true; 
    } 



    if (error) { 
     e.preventDefault(); 
     } 
    }); 
}); 
+0

更少的代碼,更多的描述你的文件系統和這些文件之間的關係會有所幫助。這似乎只是一個簡單的問題與路徑。任何404在您的控制檯? – thescientist

回答

3

更改您加載它們的順序:

<script src="jquery-1.10.2.js"></script> 
<script src="formValidation.js"></script> 
+0

儘可能簡單,謝謝。所以Jquery優先於我的自定義腳本? – user1829823

+2

不,您的自定義腳本需要jQuery才能正常工作。腳本從上到下執行。 –

0

在其中加載腳本的順序很重要。因爲你的腳本使用jquery,所以jquery需要在你的腳本之前加載。 這樣改變加載順序。首先加載jQuery,然後你的腳本。

這也是原因,爲什麼你的腳本工作內聯。