[英]How to call a variable from nested function in Jquery
I wanted to get the value of chkArray variable when called outside the method. 我想在方法外调用时获取chkArray变量的值。
When I called getValues().total from outside the method I am getting a error message as cannot read property total of undefined. 当我从方法外部调用getValues()。total时 ,我收到一条错误消息,因为无法读取未定义的属性总计。
function getValues(){
chkArray = new Array() ;
$("input[type=checkbox]:checked").each(function fire() {
var total = chkArray.push($(this).val());
console.log(chkArray)
});
};
Kindly, help how to call that variable outside the method 请帮助如何在方法外调用该变量
Use this: 用这个:
function getValues(){
var total = new Array() ;
$("input[type=checkbox]:checked").each(function fire() {
total.push($(this).val());
});
return total;
};
Call function 通话功能
var total= getValues();
console.log(total);
function getValues() { var total = new Array(); $("input[type=checkbox]:checked").each(function() { total.push($(this).val()); }); return total; }; function doSomething() { var k = getValues(); console.log(k); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 1 <input type="checkbox" name="myCheckbox" value="1" /> <br />2 <input type="checkbox" name="myCheckbox" value="2" /> <br />3 <input type="checkbox" name="myCheckbox" value="3" /> <br />4 <input type="checkbox" name="myCheckbox" value="4" /> <br />5 <input type="checkbox" name="myCheckbox" value="5" /> <br /> <input type="button" onClick="doSomething()" value="Click me" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.