jQuery adding 2 numbers from input fields

前端 未结 6 1470
梦谈多话
梦谈多话 2020-12-16 15:29

I am trying to add two values of alert boxes but I keep getting a blank alert box. I don\'t know why.

$(document).ready(function(){
    var a = $(\"#a\").va         


        
6条回答
  •  时光说笑
    2020-12-16 16:25

    Ok so your code actually works but what you need to do is replace a and b in your click function with the jquery notation you used before the click. This will ensure you have the correct and most up to date values. so changing your click function to this should work:

    $("submit").on("click", function(){
        var sum = $("#a").val().match(/\d+/) + $("#b").val().match(/\d+/);
        alert(sum);         
    })
    

    or inlined to:

    $("submit").on("click", function(){
        alert($("#a").val().match(/\d+/) + $("#b").val().match(/\d+/));         
    })
    

提交回复
热议问题