See Javascript File in JQuery

I have simple HTML. I am using jQuery for the purpose of AJAX. Now I want to put the javascript function in a separate javascript file. What is the syntax for this? For example, currently my section of the script in HTML looks something like this:

<script>
<script type="text/javascript" src="scripts/scripts.js"></script>
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type = "text/javascript" language="javascript">
 $(document).ready(function() {
   $("#SubmitForm").click(Submit());
 });
</script>

But I want to put a function

function() {
   $("#SubmitForm").click(Submit());
 })

in the scripts.js file. Can I use the name assignment of this function and refer to it?

EDit: I still have a problem: I changed the code to

<script type = "text/javascript" language="javascript">
$(document).ready(function() {
    $("#SubmitForm").click(submitMe);
});
</script>

and in a separate js file, I have the following code:

var submitMe = function(){
alert('clicked23!');
//$('#Testing').html('news');
};

Here's the body section:

<body>
welcome
<form id="SubmitForm" action="/showcontent" method="POST">
<input type="file" name="vsprojFiles" />
<br/>
<input type="submit" id="SubmitButton"/>
</form>

<div id="Testing">
hi
</div>

</body>

However, it still does not work, is something missing?

+3
source share
4 answers

scripts.js script jQuery script, script scripts.js. jQuery scripts.js, Javascript inline , .

$("#SubmitForm").click(Submit());

$("#SubmitForm").click(Submit);

, , , , .

, $(document).ready( $( ie:

 $(function() {
   $("#SubmitForm").click(Submit);
 });

$ document.ready ( ), jQuery.js , ( DOM) - , . scripts.js, , script.

, , :

var func = function() {
  $("#SubmitForm").click(Submit);
};

$(func);

, , , , scripts.js(, , , .

( ). , . , . "#SubmitButton", :

$("#SubmitButton").click(Submit);
+5

, , , .

var myfunc = function(){
    // Do some stuff
}

jQuery:

$(document).ready(myfunc);

, , jQuery:

$(myfunc);
+1

$(document).ready(function() {
    $("#SubmitForm").click(Submit);
});

? , , onDomReady.

html ( , <script> ):

<script type="text/javascript" src="scripts/scripts.js"></script>
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/scripts.js"></script>
0
$('head').append('&lt;script type="text/javascript" src="scripts/scripts.js"/&gt;')
-4

Source: https://habr.com/ru/post/1697967/


All Articles