简体   繁体   中英

Issue on Registering JavasCript Files

I have two very simple files : email.js and script.js

the email.js is

$(function () {
    function txtInput(elem) {
      var inputData = $.trim(elem.val());
      if (inputData == "") {
        alert("Filed Is Empty");
      }
      else{
        return inputData;
      }
    }
});

and the script.js

$(function () {
 var proceed = false;
    $("#btnContactUs").on("click", function (e) {
    if (txtInput($('#name'))){
     proceed = true;
    }else{ proceed = false }
       e.preventDefault();
    });
});

which I registered them in index.php as

<script src="js/email.js"></script>
<script src="js/script.js"></script>

Issues:

1 - First of all the $("#btnContactUs").on("click", function (e) {}); is not doing any thing so I removed the

$(function () {

});

from the email.js and it started working! Can you please let me know why the $(function () {}); is doing this?

2 - Now the Code is functioning(after removing the $(function () ... from email.js) BUT I am getting a warning/error in the console as:

在此处输入图片说明

Can you please let me know why this is happening? and how can I fix it?

Thanks,

When functions and variables declared with var are declared within other functions, they are local to their parent functions. That's why txtInput was not defined until you removed $(function(){}); .

This should have cleared the "not defined" error, especially since your code is now functioning. Clear the console, refresh the page, and see if it still shows the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM