简体   繁体   中英

get the return value of function in javascript

I have a 2 files. file1 and file2. In file1 i have input box. so, i am getting this value in a javascript function like this

     <td>
         <input  onmouseleave="detect(this.value)" id="myqty"  value="'.$items['qty'].'">
     </td>
     <script>
            var retVal; 
            function detect(value)
            {

                 retVal=value;
                 return retVal

             }
       </script>  

I am returning that value. So in file2 i have javascript where i want to receive that returned value. how can i?

file2

 $(document).on('mouseleave', '#myqty', function(e){
  e.preventDefault();
   var returnValue= detect();
});

but in var returnValue= detect(); i am getting nothing.

Maybe its because function is expecting a parameter. Try this instead:-

$(document).on('mouseleave', '#myqty', function(e){
  e.preventDefault();
   var returnValue= detect($(this).val());
});

You should use class and this how your code can be tweaked and updated:

 $(document).on('mouseleave', '.myqty', function(e) { e.preventDefault(); console.log($(this).val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <td> <input class="myqty" value="123"> </td> <td> <input class="myqty" value="324"> </td> <td> <input class="myqty" value="36546"> </td> <td> <input class="myqty" value="654656"> </td> 

You need to pass parameter in here var returnValue= detect();

First you dont need 2 functions for this. Why are you using 2 functions for this. You want quantity in another js file you can do all the code there only.

 $(document).ready(function(){
        $('#myqty').mouseleave(function(){

                var retVal= $('#myqty').val();
                alert(retVal);
        });
 });

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