Id ning jQuery elementini ko'paytirish

Ushbu oddiy kod bor:

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

$('[id^=pz]').change(function() {

  //Getting the numbers after the letters "pz":
  //1
  //2
  //100

});
<input type="text" id="pz1">
<input type="text" id="pz2">
<input type="text" id="pz100">
</div> </div>

Har bir element raqami qanday olinadi?

Rahmat.

1
bor num = bu.id.substr (2); !
qo'shib qo'ydi muallif ibrahim mahrir, manba
Rahmat sizga juda katta rahmat
qo'shib qo'ydi muallif eawedat, manba

8 javoblar

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

$('[id^=pz]').change(function() {

console.log($(this).attr("id").split("pz")[1])

    }); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="pz1">
<input type="text" id="pz2">
<input type="text" id="pz100">
</div> </div>
1
qo'shib qo'ydi

On changeing the input you have to use document.querySelectorAll() to get all the elements first.Then use forEach to get the value of id for each element and match the require numbers using regular expression. <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

    $('[id^=pz]').change(function() {
    var pz = document.querySelectorAll('[id^=pz]');
    values = [];
    pz.forEach(function(pz){
    values.push(pz.getAttribute('id'));
    })
    values.forEach(function(value){
    console.log(value.match(/(?!pz)\d+/g)[0]);
    });
        }); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="pz1">
    <input type="text" id="pz2">
    <input type="text" id="pz100">
</div> </div>
1
qo'shib qo'ydi
@epascarello men uni tahrirladim.
qo'shib qo'ydi muallif Hek mat, manba
Nima uchun jQuery va DOMni aralashtirasiz?
qo'shib qo'ydi muallif epascarello, manba

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

$('[id^=pz]').change(function() {

  
 //Getting the numbers after the letters "pz":
  console.log(this.id.replace("pz", ""));

}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="pz1">
<input type="text" id="pz2">
<input type="text" id="pz100">
</div> </div>
1
qo'shib qo'ydi
Kechirasiz! ahmoqona xato Men savolni tahrir qilyapman deb o'yladim!
qo'shib qo'ydi muallif ibrahim mahrir, manba

match funksiyasidan foydalanishingiz mumkin:

var m = $(this).attr('id').match("pz([0-9]+)");//m = ["pz1", "1"]
var num = m[1];

match takes a regex and returns the input matching the regex as well as possible capturing groups (i.e. the things matching the parts in parentheses).

Eslatma: so'nggi jQuery'da attr() o'rniga prop() dan foydalaning.

1
qo'shib qo'ydi

Buni qilishni;)

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

$('[id^=pz]').change(function(event) {

// with substr must have 2 chars
var id = event.target.id.substr(2);
console.log('substr', id)

// with regexp
var id2 = event.target.id.match('[0-9]+')[0];
console.log('regexp', id2)


    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<input type="text" id="pz1">
<input type="text" id="pz2">
<input type="text" id="pz1000000">
<input type="text" id="pzdsfg1000000">
</div> </div>
1
qo'shib qo'ydi
@eawedat Mening javobimni tekshiring. pz dan farqli so'z bo'lsa ham ishlaydi.
qo'shib qo'ydi muallif kind user, manba
katta rahmat
qo'shib qo'ydi muallif eawedat, manba
kalimada ikkita harfdan ko'p bo'lsa nima bo'ladi? pzez300 kabimi?
qo'shib qo'ydi muallif eawedat, manba
@Steeve Pitis, hodisani parametr sifatida qabul qilishim kerakmi? Men faqat this.id.match ('[0-9] +)' dan foydalana olmadim) [0]; ?
qo'shib qo'ydi muallif eawedat, manba
go'yo, yo'q ... buni sinab ko'ring!
qo'shib qo'ydi muallif Steeve Pitis, manba
Bu sizning original savolingizda emas edi: p, bu holda regexp dan foydalaning javobni yangilayman
qo'shib qo'ydi muallif Steeve Pitis, manba
Jquery-dan foydalanayotganingizdan ishonch hosil qilishingiz mumkin. Men uni ishlatmayman, shuning uchun;)
qo'shib qo'ydi muallif Steeve Pitis, manba

Ma'lumot idorasi sifatida identifikatsiyani olish uchun yaxshiroq yechim bo'lishi mumkin, shuning uchun identifikatordan o'chirishga hojat yo'q.

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

$("[data-pz]").on("change", function(){
    var elem = $(this);
    console.log(elem.data("pz"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input data-pz="1"/>
<input data-pz="10"/>
<input data-pz="100"/>
</div> </div>
0
qo'shib qo'ydi

Mana. Oddiy RegExp bilan Array # match dan foydalaning.

Hisob raqamini ochish uchun biron bir uskuna bosing.

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

$('input').click(function(){
  console.log($(this).attr('id').match(/(?!pz)\d+/g)[0]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="pz1">
<input type="text" id="pz2">
<input type="text" id="pz100">
<input type="text" id="pwadawdz7">
<input type="text" id="random98">
</div> </div>
0
qo'shib qo'ydi
"Men sizning kelajagingizni ishlab chiquvchi deb bilaman, shuning uchun siz o'z kodimni ishlatishni taklif qilaman" Bu yaxshi hazil, kulish uchun tashakkur.
qo'shib qo'ydi muallif epascarello, manba
Men tanladim, chunki u tanlaganlarni so'rab yolvorgan. Sizning qaroringiz muntazam ifoda ishlatadigan boshqalardan yaxshiroqdir.
qo'shib qo'ydi muallif epascarello, manba
Sizga juda minnatdorman +1
qo'shib qo'ydi muallif eawedat, manba
lol :) Ha siz haqsiz, sizning javobingiz eng yaxshisidir, siz haqiqatan ham bunga loyiqsiz,
qo'shib qo'ydi muallif eawedat, manba

Quyidagi kabi muntazam ifodalardan foydalanishingiz mumkin:

$.each($('[id^=pz]'), function (_, item) {
  var match = item.id.match(/pz(\d+)/);
  if(match) {
    console.log(match[1]);  
  }
});
0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb