Javascript: matn maydonini o'chirish ishlamaydi

Jsfiddle'da kodni sinab ko'rganimda kod to'g'ri ishlashiga qaramay, lekin men uni loyihada foydalanganimda, ba'zi bir javascriptni form uchun formatlashni sinab ko'rdim. ishlamang. Kimdir mening kodimdagi xatoni tushuntira oladimi?

<script>
function matchWord(e){
if(document.getElementById('uname').value.trim === 'Unknown' || document.getElementById('uname').value.trim === 'unknown'){
    document.getElementById('pword').disabled = true;
    document.getElementById('response').disabled = true;
    alert('you enter bannned name in the field');
 }
else{
 }
}
document.getElementById('uname').onkeyup = matchWord();
</script>

<html>
   <body>
       Name:<input type="text" name="uname" id="uname" placeholder="Username">
password:<input type="password" name='pword' id='pword' placeholder="password">
Responsibility: </body> </html>
1
Skriptni tanaga qo'yib ko'ring. Yoki hech bo'lmaganda <html> tegida! Tartibga solish: xavfsizligini ta'minlash uchun <head> tagini bering.
qo'shib qo'ydi muallif Adam Patterson, manba
Skriptni tanaga qo'yib ko'ring. Yoki hech bo'lmaganda <html> tegida! Tartibga solish: xavfsizligini ta'minlash uchun <head> tagini bering.
qo'shib qo'ydi muallif Adam Patterson, manba
Skriptni tanaga qo'yib ko'ring. Yoki hech bo'lmaganda <html> tegida! Tartibga solish: xavfsizligini ta'minlash uchun <head> tagini bering.
qo'shib qo'ydi muallif Adam Patterson, manba
Typo: funktsiya matchWord (camelcase) va boshqalar = matchword (kichik harf).
qo'shib qo'ydi muallif Jonathan Lonowski, manba
Brauzeringizning devtools-larini xatolarga tekshirdingizmi? Buning biri " Nima uchun jQuery yoki getElementById kabi DOM usuli elementni topmasa? "
qo'shib qo'ydi muallif Jonathan Lonowski, manba
Typo: funktsiya matchWord (camelcase) va boshqalar = matchword (kichik harf).
qo'shib qo'ydi muallif Jonathan Lonowski, manba
faqat uni tuzatib qo'ying, lekin hali ham ishlamaydi.
qo'shib qo'ydi muallif Nexz, manba
faqat uni tuzatib qo'ying, lekin hali ham ishlamaydi.
qo'shib qo'ydi muallif Nexz, manba

9 javoblar

First trim() is not a property, its a method. So, you need to change it from trim to trim().

Second you need to assign a function to onkeyup, not call a function.

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

function matchWord(e) {
    if (document.getElementById('uname').value.trim() === 'Unknown' || document.getElementById('uname').value.trim === 'unknown') {
        document.getElementById('pword').disabled = true;
        document.getElementById('response').disabled = true;
        alert('you enter bannned name in the field');
    } else {}
}
document.getElementById('uname').onkeyup = matchWord;
Name:<input type="text" name="uname" id="uname" placeholder="Username">
password:<input type="password" name='pword' id='pword' placeholder="password">
Responsibility:
</div> </div>
2
qo'shib qo'ydi

Problem 1: Typo matchword(), replace it with matchWord();

Muammo 2: trim - bu usul! uning o'rniga document.getElementById ('uname') dan foydalaning. value.trim() o'rniga.

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

function matchWord(e) {
  if (document.getElementById('uname').value.trim() === 'Unknown' || document.getElementById('uname').value.trim() === 'unknown') {
    document.getElementById('pword').disabled = true;
    document.getElementById('response').disabled = true;
    alert('you enter bannned name in the field');
  }
}
Name:
<input type="text" name="uname" id="uname" placeholder="Username" onkeyup="matchWord()">

password: <input type="password" name='pword' id='pword' placeholder="password">
Responsibility:
</div> </div>
2
qo'shib qo'ydi

Siz bergan namunada bir nechta xato mavjud.

  • Birinchidan, nomlar katta-kichikligi farqlanadi, shuning uchun matchWord kodi matchword bilan bir xil emas. Ehtiyot bo'ling!
  • Ikkinchidan, funksiyani o'zi qaytib keladigan natijaga emas, balki voqea uchun belgilash uchun document.getElementById ("uname") ni ishlatishingiz kerak.
  • Uchinchi va oxirgi nuqta, sizning usulingizdagi mantiq noto'g'ri, chunki trim() o'rniga trim() foydalanasiz, yuqoridagi funktsiyani o'zgartiradi va bu funksiya qiymati o'rniga funksiyani qaytaradi. Quyidagi snippet ishlaydi:

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

function matchWord(e) {
  if (document.getElementById('uname').value.trim() === 'Unknown' ||
    document.getElementById('uname').value.trim() === 'unknown') {
    document.getElementById('pword').disabled = true;
    document.getElementById('response').disabled = true;
    alert('you enter bannned name in the field');
  } else {}
}

document.getElementById('uname').onkeyup = matchWord;
Name:<input type="text" name="uname" id="uname" placeholder="Username">
password: <input type="password" name='pword' id='pword' placeholder="password">
Responsibility:
</div> </div>
1
qo'shib qo'ydi
@Nexz bu qanday amalga oshadi? Uni ishga tushirganingizda nima sodir bo'ladi?
qo'shib qo'ydi muallif Angelos Chalaris, manba
@Nexz siz qaysi brauzerni yoqasiz? Bu zamonaviy brauzerlarda juda yaxshi ishlaydi ...
qo'shib qo'ydi muallif Angelos Chalaris, manba
hali ham muvaffaqiyatsiz tugadi, muammo brauzerimdan. hali ham rahmat.
qo'shib qo'ydi muallif Nexz, manba
OK, men javobni topdim. Lekin fyi, men xrom va firefox.thanks dan foydalanaman.
qo'shib qo'ydi muallif Nexz, manba
nomni yozayotganimda ogohlantirish funksiyasi ishlamaydi, shuningdek, uning ostidagi ikkala maydonni ham o'chirmaydi. Sizning kodingiz ishlamayotganligini aytmayapman, lekin bu hali hammasi kod qismlari vositasida ishlaydi, faqat brauzerimda.
qo'shib qo'ydi muallif Nexz, manba

KeyUp hodisasining voqealaridan biri sizning vazifangiz natijalarini belgilaydi va siz uni funktsiyani belgilashni xohlaysiz.

Sinash:

document.getElementById('uname').onkeyup = matchWord;

Buning o'rniga yoki hodisa ishlovchilarini elementning o'zida qo'shish, oldingi satrni olib tashlash va qo'shib qo'yish:

<input type="text" name="uname" id="uname" placeholder="Username" onkeyup="matchWord()">
1
qo'shib qo'ydi

KeyUp hodisasining voqealaridan biri sizning vazifangiz natijalarini belgilaydi va siz uni funktsiyani belgilashni xohlaysiz.

Sinash:

document.getElementById('uname').onkeyup = matchWord;

Buning o'rniga yoki hodisa ishlovchilarini elementning o'zida qo'shish, oldingi satrni olib tashlash va qo'shib qo'yish:

<input type="text" name="uname" id="uname" placeholder="Username" onkeyup="matchWord()">
1
qo'shib qo'ydi

Qo'y:

<script>//stuff </script>

ichida:

<html>//here </script>
0
qo'shib qo'ydi

Qo'y:

<script>//stuff </script>

ichida:

<html>//here </script>
0
qo'shib qo'ydi
Below code works fine for me in all browsers

<html>
   <body>
       Name:<input type="text" name="uname" id="uname" placeholder="Username">
password:<input type="password" name='pword' id='pword' placeholder="password">
Responsibility: </body> <script> function matchWord(e){ if(document.getElementById('uname').value.trim() === 'Unknown' || document.getElementById('uname').value.trim() === 'unknown'){ document.getElementById('pword').disabled = true; document.getElementById('response').disabled = true; alert('you enter bannned name in the field'); } else{ } } document.getElementById('uname').addEventListener("keyup", function(evt) { matchWord(evt); }, false); </script> </html>
0
qo'shib qo'ydi
Maslahat: parcha bilan birga kamida qisqacha tushuntirishni qo'shishga harakat qiling, bu savoldagi parchadan boshqa narsalarni batafsil tavsiflab bering va nega revizyonlar kerak deb o'ylaysiz.
qo'shib qo'ydi muallif Jonathan Lonowski, manba
thanks, kodingiz men uchun ishlaydi.
qo'shib qo'ydi muallif Nexz, manba
Below code works fine for me in all browsers

<html>
   <body>
       Name:<input type="text" name="uname" id="uname" placeholder="Username">
password:<input type="password" name='pword' id='pword' placeholder="password">
Responsibility: </body> <script> function matchWord(e){ if(document.getElementById('uname').value.trim() === 'Unknown' || document.getElementById('uname').value.trim() === 'unknown'){ document.getElementById('pword').disabled = true; document.getElementById('response').disabled = true; alert('you enter bannned name in the field'); } else{ } } document.getElementById('uname').addEventListener("keyup", function(evt) { matchWord(evt); }, false); </script> </html>
0
qo'shib qo'ydi
Maslahat: parcha bilan birga kamida qisqacha tushuntirishni qo'shishga harakat qiling, bu savoldagi parchadan boshqa narsalarni batafsil tavsiflab bering va nega revizyonlar kerak deb o'ylaysiz.
qo'shib qo'ydi muallif Jonathan Lonowski, manba
thanks, kodingiz men uchun ishlaydi.
qo'shib qo'ydi muallif Nexz, manba
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