2013-03-17 10 views
13

Ho il seguente codice rasoio Asp.Net MVC 4 alla fine del file.

.... 
@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

<script type="text/javascript"> 
    $('#Address_State').val('MA'); 
</script> 
////// End of file 

Tuttavia, ha generato il seguente codice html. E solleva un errore sulla linea $('#Address_State').val('MA');. Il messaggio di errore è Uncaught ReferenceError: $ is not defined. Come inserire il codice jQuery nel file del rasoio?

..... 
<script type="text/javascript"> 
    $('#Address_State').val('MA'); // Uncaught ReferenceError: $ is not defined 
</script> 
      </section> 
     </div> 
     <footer> 
      <div class="content-wrapper"> 
       <div class="float-left"> 
        <p>&copy; 2013 - Paperspeed</p> 
       </div> 
      </div> 
     </footer> 

     <script src="/Scripts/jquery-1.9.1.js"></script> 


    <script src="/Scripts/jquery.unobtrusive-ajax.js"></script> 
<script src="/Scripts/jquery.validate.js"></script> 
<script src="/Scripts/jquery.validate.unobtrusive.js"></script> 


    </body> 
</html> 

Aggiornamento:

Ciò che segue è le ultime quattro righe di _Layout.cshtml.

 @Scripts.Render("~/bundles/jquery") 
     @RenderSection("scripts", required: false) 
    </body> 
</html> 
+0

Si dovrebbe spostare lo script per il fondo. – CodeColorist

+0

@ChiChou Lo script jquery si trova già nella parte inferiore del file. – ca9163d9

+0

Intendevo il tuo script, non jQuery. Vedi quelle risposte qui sotto. – CodeColorist

risposta

23

È necessario includere JQuery prima di utilizzarlo effettivamente.

Un modo comune per ottenere questo è nel layout principale, inclusi gli script di richiesta in testa. O posizionare una sezione facoltativa (testa) in cui è possibile condizionale includere script

prima creare un pacchetto necessario per gli script jQuery si desidera in ogni pagina:

public static void RegisterBundles(BundleCollection bundles) 
{ 
    bundles.Add(new ScriptBundle("~/js/required").Include(
     "~/Scripts/jquery-2.0.2.js")); 

    //other bundles 
} 

quindi creare un modello di sito:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>Title</title> 
    @Styles.Render("~/css/required") 
    @RenderSection("head", false) //For css on a page by page basis 
</head> 
<body> 
    @RenderBody() 
    @Scripts.Render("~/js/required") //Here add your jquery include 
    @RenderSection("scripts", false) //Here you add scripts at the bottom of the page 
</body> 
</html> 

quindi utilizzare questo file di rasoio come il layout di base per tutte le altre viste rasoio derivati ​​come questo:

@{ 
    ViewBag.Layout = "~/Views/Shared/_MasterLayout.cshtml"; 
} 

@section head { 
    //Any css you want to add 
} 

<p>Some html content</p> 

@section scripts { 
    //scripts you want to include at the bottom on a page by page basis 
} 
5

vostro script si chiama prima che la jQuery è anche caricata in modo che non sa cosa $ mezzi. Prova a caricare gli script jquery nella sezione head del file di layout.

10

Sposta le seguenti righe di codice nella parte superiore della tua vista _Layout.cshtml all'interno del tag body. Quindi gli script jQuery caricheranno per primi.

<body> 
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/bundles/bootstrap") 
    @RenderSection("scripts", required: false) 
+1

questa è la risposta più accurata per questo problema. –

0

jquery add è dopo il rendering del corpo? si sposta seguendo "

@Scripts.Render("~/bundles/jquery") 
@RenderSection("scripts", required: false) 

" linee di tag

come questo "

@Scripts.Render("~/bundles/jquery") 
@RenderSection("scripts", required: false) 
</head> 

"

Problemi correlati