tugmachasini bosish jquery-da ishlamaydi

Salom bolalar jQuery uchun yangi.

Jquery-da oddiy qo'shimchani sinab ko'rmoqdaman. Mening ikkita kirish maydonim bor va jQuery da bu sohada raqamlarni qo'shishni xohlayman. lekin tugmacha qo'shilmaydi.

JQuery

<script type="text/javascript">
    $(document).ready(function() {
        //toggle div start
        $("#btn_do").click(function() {
            $(".div_chat").animate({
                width: "toggle"
            });
        });
       //toggle div end

        //Add button click
        $("btnplus").click(function() {
            alert("sdfdf");
            var val1 = $("#lblvalue1").val();
            var val2 = $("#lblvalue2").val();
            var added = parseInt( val1 )+ parseInt( val2);
            alert(added2);
            $("lbltext").val(added);
        });
        //add button click end

    });
</script>

HTML

 <form id="form1" runat="server">
    
    <div>
        
        <button id="btn_do" type="button">toggle</button>
    </div>
    <div></div>
    <div id="divchatID" class="div_chat" style="height: 500px; width: 500px; background-color: #82adf2; display: none; padding:5px;">
        
        <input id="value1" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />
        
<input id="value2" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' /> <button id="btnplus" type="button" > ADD</button>
tesst </div> </form>

kimdir oldindan rahmat.

1
$ ("btnplus") tugmachasini bosing (function() {) $ ("# btnplus") bo'lishi kerak (Function() {.
qo'shib qo'ydi muallif Alive to Die, manba
Vaqar ahmed sulton so'radingiz, men javobni qo'shdim. Iltimos, qarash
qo'shib qo'ydi muallif Alive to Die, manba
uffffffff ... nima achinarli xato. rahmat
qo'shib qo'ydi muallif waqar ahmed somra, manba
iltimos, javob sifatida qo'shib qo'ying, men buni qabul qila olaman. rahmat
qo'shib qo'ydi muallif waqar ahmed somra, manba
Men qabul qilingan javobni ko'ra olmayapman. Men uni yana qabul qilaman. :)
qo'shib qo'ydi muallif waqar ahmed somra, manba

9 javoblar

Kodingiz to'g'ri ishlamasligi uchun bir nechta muammo yuz berdi. Bularning barchasini pastdagi qismga joylashtirdim. Xulosa:

  • Your button has an ID of btnplus. To reference IDs in jQuery queries, you need to use the # prefix, i.e. $("#btnplus")
  • your variable added2 is not defined. This should probably be added.
  • val() should be called on the input tag, i.e. #value1, not the label tag
  • best practice is to call parseInt(x, 10) rather than parseInt()
  • ID in label should be id lowercase.
  • $("lbltext") should be $("#lbltext")

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

//toggle div start
$("#btn_do").click(function() {
  $(".div_chat").animate({
    width: "toggle"
  });
});
// toggle div end

//Add button click
$("#btnplus").click(function() {
  alert("sdfdf");
  var val1 = $("#value1").val();
  var val2 = $("#value2").val();
  var added = parseInt(val1, 10) + parseInt(val2, 10);
  alert(added);
  $("#lbltext").text(added);
});
//add button click end
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" runat="server">
  
  <div>
    
    <button id="btn_do" type="button">toggle</button>
  </div>
  <div></div>
  <div id="divchatID" class="div_chat" style="height: 500px; width: 500px; background-color: #82adf2; display: none; padding:5px;">
    
    <input id="value1" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />
    
<input id="value2" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' /> <button id="btnplus" type="button"> ADD</button>
tesst </div> </form>
</div> </div>
3
qo'shib qo'ydi

You were missing # from $("btn_do")

Also, var val1 = $("#lblvalue1").val(); is wrong since, you have no Id with lblvalue1, it should be $("#value1") Same goes for value2

qo'shilgan2 dan boshlab, oxirgi "typeo" alert (add2); mavjud emas

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

$(document).ready(function() {
  //toggle div start
  $("#btn_do").click(function() {
    $(".div_chat").animate({
      width: "toggle"
    });
  });
 //toggle div end

  //Add button click
  $("#btnplus").click(function() {
    alert("sdfdf");
    var val1 = $("#value1").val();
    var val2 = $("#value2").val();
    var added = (parseInt(val1) + parseInt(val2));
    alert(added);
    $("lbltext").val(added);
  });
  //add button click end

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" runat="server">
  
  <div>
    
    <button id="btn_do" type="button">toggle</button>
  </div>
  <div></div>
  <div id="divchatID" class="div_chat" style="height: 500px; width: 500px; background-color: #82adf2; display: none; padding:5px;">
    
    <input id="value1" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />
    
<input id="value2" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' /> <button id="btnplus" type="button"> ADD</button>
tesst </div> </form>
</div> </div>
3
qo'shib qo'ydi

Siz bu erda identifikatorni unutdingiz ( # )

$ ("# btnplus") ni bosing (function() {

Shuningdek, u erda kerakli o'zgartirishni: -

$("#btnplus").click(function() {
    var val1 = $("#value1").val();//change of id
    var val2 = $("#value2").val();//change of id
    var added = parseInt( val1 )+ parseInt( val2);
    alert(added);//change of variable name
   $("span[id$='lbltext']").text(added);
});

Ishlaydigan misol: -

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

$(document).ready(function() {
    //toggle div start
    $("#btn_do").click(function() {
        $(".div_chat").animate({
            width: "toggle"
        });
    });
   //toggle div end

    //Add button click
    $("#btnplus").click(function() {
        var val1 = $("#value1").val();
        var val2 = $("#value2").val();
        var added = parseInt( val1 )+ parseInt( val2);
        alert(added);
       $("span[id$='lbltext1']").text(added);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <form id="form1" runat="server">
    
    <div>
        
        <button id="btn_do" type="button">toggle</button>
    </div>
    <div></div>
    <div id="divchatID" class="div_chat" style="height: 500px; width: 500px; background-color: #82adf2; display: none; padding:5px;">
        
        <input id="value1" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />
        
<input id="value2" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' /> <button id="btnplus" type="button" > ADD</button>
<!--tesst--> </div> </form>
</div> </div>
3
qo'shib qo'ydi
$("#btnplus").click(function() {
        alert("sdfdf");
        var val1 = $("#lblvalue1").val();
        var val2 = $("#lblvalue2").val();
        var added = parseInt( val1 )+ parseInt( val2);
        alert(added);
        $("#lbltext").val(added);
    });
3
qo'shib qo'ydi

Kodingizda # ni unutdingiz. Buni ko'ring:

$("#btnplus").click(function() {
            alert("sdfdf");
            var val1 = $("#lblvalue1").val();
            var val2 = $("#lblvalue2").val();
            var added = parseInt( val1 )+ parseInt( val2);
            alert(added);
            $("#lbltext").val(added);
        });
3
qo'shib qo'ydi

Click voqeasini qo'llash uchun # -ni ishlatishingiz kerak.

$("#btnplus").click(function() {
    alert("sdfdf");
    var val1 = $("#lblvalue1").val();
    var val2 = $("#lblvalue2").val();
    var added = parseInt( val1 )+ parseInt( val2);
    alert(added2);
    $("lbltext").val(added);
});
3
qo'shib qo'ydi
replace the code,
$("#btnplus").click(function() {
        alert("sdfdf");
        var val1 = $("#value1").val();
        alert(val1);
        var val2 = $("#value2").val();
        var added = parseInt( val1 )+ parseInt( val2);
        alert(added);
        $("lbltext").val(added);
    });
2
qo'shib qo'ydi
Faqat ishlaydigan kodni o'zgartiring, ba'zi idlarni o'tkazib yubordingiz,
qo'shib qo'ydi muallif Kaushar Alam, manba

JQuery dan foydalanayotganda id va sinf selektorlariga qo'shilganda ishonch hosil qiling. ("#" va ".").

$("#btnplus").click(function() {
   //code
});

Bundan tashqari, bu erda yo'qolgan:

("#lbltext").val(added);
2
qo'shib qo'ydi

Siz btnplus tugma raqami sifatida berdingiz. Va siz bu kodni tanlash sifatida ishlatasiz, shu tugmani bosish uchun avval # belgisini ishlatishingiz kerak.

$("#btnplus").click(function() {
   //Your code goes here
});
1
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