$post jQuery中使用的方法

$post method using in jQuery

本文关键字:方法 post jQuery      更新时间:2023-09-26

我想在我的jQuery计算器中创建一个"添加"按钮。当我单击"添加"按钮时,它会在显示屏上显示"+",并且将存储我输入的数字。之后,我可以输入另一个数字来完成等式。我可以卡在添加按钮的部分,不知道该怎么做。我需要使用 load() 吗?

试试这个。用您提供的有限输入制定解决方案

http://jsfiddle.net/sabkaraja/utc7f2ex/

您可以决定要对 #add.click(....) 事件中的附加值执行哪些操作。我使用了一个简单的评估来获得结果。

 $(function () {
     var $display = $('#display');
     $display.val(0);

     $(document).on('click', 'button.number', function () {
         if ($display.val().length >= 8) {
             $display.val("Error");
         } else if ($display.val() == "Error") {
             $display.val("0");
         } else {
            $display.val( $display.val() + '+' + $(this).val());
         }
     });
     $("#clear").click(function () {
         $display.val("0");
     });
     $("#ce").click(function () {
         if ($display.val().length >= 2) {
             $display.val($display.val().substring(0, $display.val().length - 1));
         } else {
             $("#ce").trigger("click");
         }
     });
     $("#add").click(function () {
         if ($display.val().length !== 0) {
             v = eval($display.val()); //<----- here is where I add the numbers
             $display.val( v); //------------> do whatever you like to do after this
             $.ajax({
                    url: 'submit.php',
                    type: 'POST',
                    dataType :'html',
                    data: {sum: v},
                    success: function(data) {
                        alert(data);
                    }
             });
         }
     });
 });