$ (Selector) dan .each ()

Mana HTML fragmenti


Mana skript

$('select option').each(function(index, elem) {
    if (elem.prop('selected')) {
       //Find a checkbox somewhere in the page that correspond with the option
       //and then check it.
    }
});

Bu esa elem.prop funktsiyasi funktsiyasi emas.

Hatto oddiy console.log (elem.prop ("tanlangan")) ishlamasa ham.

Bunga qanday o'tishim mumkin?

2
@ Baldrani 2 tanlovi tanlangan
qo'shib qo'ydi muallif guradio, manba
Siz oddiygina $ ('option: selected') dan foydalana olmaysizmi?
qo'shib qo'ydi muallif Baldráni, manba
Siz oddiygina $ ('option: selected') dan foydalana olmaysizmi?
qo'shib qo'ydi muallif Baldráni, manba
Rory McCrossan. Ha. Men uni faqat unutib qo'ydim.
qo'shib qo'ydi muallif starleaf1, manba
Rory McCrossan. Ha. Men uni faqat unutib qo'ydim.
qo'shib qo'ydi muallif starleaf1, manba
Rory McCrossan. Ha. Men uni faqat unutib qo'ydim.
qo'shib qo'ydi muallif starleaf1, manba
@guradio ni tanlang, lekin tanlovda multiple xususiyati yo'q. OP - bir nechta tanlovga ruxsat berishga harakat qilyapsizmi?
qo'shib qo'ydi muallif Rory McCrossan, manba
@guradio ni tanlang, lekin tanlovda multiple xususiyati yo'q. OP - bir nechta tanlovga ruxsat berishga harakat qilyapsizmi?
qo'shib qo'ydi muallif Rory McCrossan, manba

6 javoblar

Sizning muammoingiz elem , jQuery obyekti emas, balki Elementga tegishli bo'lgani uchundir. Sizga kerakli narsani bajarish uchun elem -ni shunday qilish kerak:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$('select option').each(function(index, elem) {
  var $elem = $(elem);
  if ($elem.prop('selected')) {
    console.log($elem.val());
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>

Biroq, sizning loopingiz keraksiz ekanligini unutmang. : tanlangan selektoridan foydalanishingiz mumkin va pastadir to'liq to'xtatiladi:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var $elem = $('select option:selected');
console.log($elem.val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>
2
qo'shib qo'ydi
Ha, amin emasman, unda nega yozma bo'lishi mumkin?
qo'shib qo'ydi muallif guradio, manba
Eslatma: 2 tanlangan variant mavjud. Menimcha, hamma variantni o'zgartirish kerak
qo'shib qo'ydi muallif guradio, manba
Xo'sh, OB multiple xususiyatini ishlatmasa ham, faqat bitta qiymat tanlanishi mumkin - oxirgi
qo'shib qo'ydi muallif Rory McCrossan, manba
Mumkin - Javobni hozirgi kabi qoldirib o'taman, o'shanda OSh o'z niyatini aniqlab berishi mumkin
qo'shib qo'ydi muallif Rory McCrossan, manba

Sizning muammoingiz elem , jQuery obyekti emas, balki Elementga tegishli bo'lgani uchundir. Sizga kerakli narsani bajarish uchun elem -ni shunday qilish kerak:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$('select option').each(function(index, elem) {
  var $elem = $(elem);
  if ($elem.prop('selected')) {
    console.log($elem.val());
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>

Biroq, sizning loopingiz keraksiz ekanligini unutmang. : tanlangan selektoridan foydalanishingiz mumkin va pastadir to'liq to'xtatiladi:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var $elem = $('select option:selected');
console.log($elem.val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>
2
qo'shib qo'ydi
Ha, amin emasman, unda nega yozma bo'lishi mumkin?
qo'shib qo'ydi muallif guradio, manba
Eslatma: 2 tanlangan variant mavjud. Menimcha, hamma variantni o'zgartirish kerak
qo'shib qo'ydi muallif guradio, manba
Xo'sh, OB multiple xususiyatini ishlatmasa ham, faqat bitta qiymat tanlanishi mumkin - oxirgi
qo'shib qo'ydi muallif Rory McCrossan, manba
Mumkin - Javobni hozirgi kabi qoldirib o'taman, o'shanda OSh o'z niyatini aniqlab berishi mumkin
qo'shib qo'ydi muallif Rory McCrossan, manba

Sizning muammoingiz elem , jQuery obyekti emas, balki Elementga tegishli bo'lgani uchundir. Sizga kerakli narsani bajarish uchun elem -ni shunday qilish kerak:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$('select option').each(function(index, elem) {
  var $elem = $(elem);
  if ($elem.prop('selected')) {
    console.log($elem.val());
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>

Biroq, sizning loopingiz keraksiz ekanligini unutmang. : tanlangan selektoridan foydalanishingiz mumkin va pastadir to'liq to'xtatiladi:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var $elem = $('select option:selected');
console.log($elem.val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>
2
qo'shib qo'ydi
Ha, amin emasman, unda nega yozma bo'lishi mumkin?
qo'shib qo'ydi muallif guradio, manba
Eslatma: 2 tanlangan variant mavjud. Menimcha, hamma variantni o'zgartirish kerak
qo'shib qo'ydi muallif guradio, manba
Xo'sh, OB multiple xususiyatini ishlatmasa ham, faqat bitta qiymat tanlanishi mumkin - oxirgi
qo'shib qo'ydi muallif Rory McCrossan, manba
Mumkin - Javobni hozirgi kabi qoldirib o'taman, o'shanda OSh o'z niyatini aniqlab berishi mumkin
qo'shib qo'ydi muallif Rory McCrossan, manba

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$('select option').each(function(index, elem) {
  if ($(elem).prop('selected')) {
   //Find a checkbox somewhere in the page that correspond with the option
   //and then check it.
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>

You should place elem in $()

1
qo'shib qo'ydi

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$('select option').each(function(index, elem) {
  if ($(elem).prop('selected')) {
   //Find a checkbox somewhere in the page that correspond with the option
   //and then check it.
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>

You should place elem in $()

1
qo'shib qo'ydi

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$('select option').each(function(index, elem) {
  if ($(elem).prop('selected')) {
   //Find a checkbox somewhere in the page that correspond with the option
   //and then check it.
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>

You should place elem in $()

1
qo'shib qo'ydi