get global variable , from function `change`
function getSelectData(id) {
var value='';
// set value to be the current selected value
value = jQuery(id+" option:selected").val();
// change value whenever the box changes
jQuery(id).change(function () {
value = jQuery(id+" option:selected").val();
});
console.log(value);
return value;
}
Thanks to @Hogan , he help me but still i can not get wanted result , how i can get variable value
, outside function change
, here is this example http://jsfiddle.net/ZvuMh/ , and every time when i'm changing select box , v开发者_JAVA百科alue is a
. Thank You for your spending time
You can't unless you move it to be a global variable. As it is, the line "console.log(value)" will only print the value before the function change() is called. (change() gets called when something changes....but that is long after getSelectData() has returned).
Here, use this as a template to solve your problem:
http://jsfiddle.net/ZvuMh/3/
精彩评论