最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access variables outside the jQuery .each() loop - Stack Overflow

programmeradmin5浏览0评论

I have a little bit plicated html structure. I parse it to get this stuff:

Option1: value
Option2: value

from this like html:

<div class="option" id="option-691">
<span class="required">*</span>
<b>Option:</b><br>
<input type="radio" checked="" id="option-value-1250" value="1250" price="1156.0000" name="option[691]">
<label for="option-value-1250">2,35 xx - 1156.00 </label>
<br>
<input type="radio" onchange="recalculateprice();reloadpr();" id="option-value-1251" value="1251" price="506.0000" price_prefix="" points="0" name="option[691]">
<label for="option-value-1251">900 xx - 506.00</label>
<br>
</div>

and this too

<div class="option" id="option-690">
<span class="required">*</span>
<b>Option:</b><br>
<select name="option[690]">
<option price="1156.0000" price_prefix="+" points="0" value="1249">value01
(+1156.00)
</option>
<option price="1156.0000" price_prefix="+" points="0" value="1248">value02
(+1156.00)
</option>
<option price="1156.0000" price_prefix="+" points="0" value="1247">value03
(+1156.00)
</option>
</select>
 </div>

And using this I can get data from both types (inputs + selects).

$('#product_options > div.option').each(function() {
 var Opt01 = ( $(this).find('option:selected').parent().parent().find('b').text() + $(this).find('option:selected').text() );
 var Opt02 = ( $(this).find('input:checked').parent().find('b').text() + $(this).find('input:checked').next().text() );

console.log(Opt01);
console.log(Opt02);
 });

But, I want to get Opt01 and Opt02 outside the .each() loop. How I can do this?

I have a little bit plicated html structure. I parse it to get this stuff:

Option1: value
Option2: value

from this like html:

<div class="option" id="option-691">
<span class="required">*</span>
<b>Option:</b><br>
<input type="radio" checked="" id="option-value-1250" value="1250" price="1156.0000" name="option[691]">
<label for="option-value-1250">2,35 xx - 1156.00 </label>
<br>
<input type="radio" onchange="recalculateprice();reloadpr();" id="option-value-1251" value="1251" price="506.0000" price_prefix="" points="0" name="option[691]">
<label for="option-value-1251">900 xx - 506.00</label>
<br>
</div>

and this too

<div class="option" id="option-690">
<span class="required">*</span>
<b>Option:</b><br>
<select name="option[690]">
<option price="1156.0000" price_prefix="+" points="0" value="1249">value01
(+1156.00)
</option>
<option price="1156.0000" price_prefix="+" points="0" value="1248">value02
(+1156.00)
</option>
<option price="1156.0000" price_prefix="+" points="0" value="1247">value03
(+1156.00)
</option>
</select>
 </div>

And using this I can get data from both types (inputs + selects).

$('#product_options > div.option').each(function() {
 var Opt01 = ( $(this).find('option:selected').parent().parent().find('b').text() + $(this).find('option:selected').text() );
 var Opt02 = ( $(this).find('input:checked').parent().find('b').text() + $(this).find('input:checked').next().text() );

console.log(Opt01);
console.log(Opt02);
 });

But, I want to get Opt01 and Opt02 outside the .each() loop. How I can do this?

Share Improve this question asked Nov 5, 2014 at 6:23 user4217275user4217275 211 gold badge1 silver badge3 bronze badges 2
  • What exactly are you trying to achieve? – Ram Commented Nov 5, 2014 at 6:25
  • In a nutshell I just want to get checked/selected values, which located in different elements. – user4217275 Commented Nov 5, 2014 at 20:49
Add a ment  | 

5 Answers 5

Reset to default 3

Variables declared within functions can't be accessed outside of them. Just declare them outside of the loop:

var Opt01;
var Opt02;

$('#product_options > div.option').each(function() {
  Opt01 = ( $(this).find('option:selected').parent().parent().find('b').text() + $(this).find('option:selected').text() );
  Opt02 = ( $(this).find('input:checked').parent().find('b').text() + $(this).find('input:checked').next().text() );

  console.log(Opt01);
  console.log(Opt02);
 });

I want to get Opt01 and Opt02 outside the .each() loop

Since the variables are local in that context (by using var keyword), outside of the each callback the variables are undefined. You could define them outside of the each callback, e.g. var opt1, opt2;, but what will happen? In each iteration the values are overridden and the last values win. If the selector returns 1 element then each shouldn't be used in the first place.

You could define a function and pass the values to it.

$('#product_options > div.option').each(function() {
    var Opt1 = '...';
    var Opt2 = '...';
    doSomething(Opt1, Opt2);
});

Or use the map method and create an array of objects:

var options = $('#product_options > div.option').map(function() {
    var opt01 = '...';
    var opt02 = '...';
    return {
       opt1: opt01,
       opt2: opt02
    }
}).get();

Just declare the variable outside the each loop. Variables declared inside the function can be accessed only from inside. JsFiddle

var Opt01, Opt02; 
function find(){
    $('#product_options > div.option').each(function() {
      var option1 = ( $(this).find('option:selected').parent().parent().find('b').text() +              $(this).find('option:selected').text() );
      var option2 = ( $(this).find('input:checked').parent().find('b').text() +      $(this).find('input:checked').next().text() );
      if(option1){ 
          Opt01 = option1;  
          console.log("inside: "+Opt01);
      }
      if(option2){  
          Opt02 = option2; 
          console.log("inside: "+Opt02); 
      }              
  });
  console.log("outside: "+Opt01);
  console.log("outside: "+Opt02);
}

It's very simple,
You have to declare your variables outside of your function as global.
But don't declare only,assign some value
Var opt1=0;

Thanks guys. I've added another validation rule before loop and code looks like this, so far. And it works fine:

if ($('#product_options div.option').length) {
  $('#product_options > div.option').each(function () {
//defining
OptionTitle = $(this).find('option:selected').parent().parent().find('b').text();
OptionValue = $(this).find('option:selected').text();
InputTitle = $(this).find('input:checked').parent().find('b').text();
InputValue = $(this).find('input:checked').next().text();
//
if (OptionTitle != '' && OptionValue != '') {
  Opt01 = (OptionTitle + OptionValue)
}
if (InputTitle != '' && InputValue != '') {
  Opt02 = (InputTitle + InputValue)
}
  });
  //get values outside the loop
console.log('Options detected');
if (typeof Opt01 === 'undefined') {
Opt01 = ''
}
if (typeof Opt02 === 'undefined') {
Opt02 = ''
}
  console.log('Opt01 = ' + Opt01);
  console.log('Opt02 = ' + Opt02);
} 
else {
console.log('Options are empty');
var Opt01 = '';
var Opt02 = '';
 console.log('Opt01 = ' + Opt01);
 console.log('Opt02 = ' + Opt02);
 }

Also, fiddle is here http://jsfiddle/caLj2xbe/

发布评论

评论列表(0)

  1. 暂无评论