2017-08-01 68 views
0

我有一个2个文件。 file1和file2。在file1中我有输入框。所以,我在这样的javascript函数中得到这个值在javascript中获取函数的返回值

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

       retVal=value; 
       return retVal 

      } 
     </script> 

我正在返回该值。所以在file2我有JavaScript的地方,我想收到返回的值。我怎么能够?

文件2

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

var returnValue= detect();我得到什么。

+2

既然你使用JQuery,你可以得到所需的值作为'$( “#myqty”)。VAL()' –

+0

我想要得到的JavaScript函数的返回值的值。我怎么能够? –

+0

你没有用这个值进行任何处理,所以当你需要'input'值时,为什么不使用'$(“#myqty”).val()'? –

回答

1

也许是因为函数期待参数。试试这个: -

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

非常感谢。它工作正常。 –

0

您应该使用class这个代码如何进行调整和更新:

$(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>

0

你需要传递的参数在这里 VAR的returnValue = detect();

首先你不需要2个功能。为什么你为此使用2个函数。 你想在另一个js文件中的数量,你可以只在那里做所有的代码。

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

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