Where to add JavaScript code that needed jQuery file?

时间:2017-08-05 11:01:09

标签: javascript jquery ajax

I've founded this article but I don't know where to add this code:

$(document).ready(function(){
  $(".showDescriptionTextbox").val($(".Product").val());//to initialize
  $(".showDescriptionDiv").text($(".Product").val());//to initialize
  $(".Product").change(function(){
    $(".showDescriptionTextbox").val($(".Product").val());
    $(".showDescriptionDiv").text($(".Product").val());
  });
});

I've file jquery-1.11.3.min.js, jquery-1.10.2.min.js.

Should I edit one of both file with above code? Please give me a hint.

I want to execute query SQL by clicking the select option that I know it's just possible using jquery/ajax/js, but I'm still newbie.

2 个答案:

答案 0 :(得分:1)

I've file jquery-1.11.3.min.js, jquery-1.10.2.min.js. Both are jquery library file with different version. Only one should be included.

In your case the code snippet which you have shared can be included in a separate js file. Like this

nameOfYourJSFile.js

$(document).ready(function(){
  $(".showDescriptionTextbox").val($(".Product").val());//to initialize
  $(".showDescriptionDiv").text($(".Product").val());//to initialize
  $(".Product").change(function(){
    $(".showDescriptionTextbox").val($(".Product").val());
    $(".showDescriptionDiv").text($(".Product").val());
  });
});

Include the file in your main html file. Add the scripts near the bottom of the page.

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
  </head>
 <body>
    <h1>Hello Plunker!</h1>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
     <script src="nameOfYourJSScript.js"></script>
  </body>
</html>

DEMO

If the file is included in the bottom , there is no need to use document.ready function

答案 1 :(得分:0)

Add your code BELOW the <script src="jquery-1.11.3.min.js"></script>

So, JQuery will be loaded and can be used