为什么我的变量不会出现在使用Jquery的TextBox中? [英] Why wont My Variable Appear In The TextBox Using Jquery?

查看:56
本文介绍了为什么我的变量不会出现在使用Jquery的TextBox中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了一些麻烦。即时建立一个计算器和即时通讯工作在它的一般想法和即时通讯混淆为什么我的变量查理不会出现在输出文本框。所有的变量最后都是正确的(我用alert函数检查过它们),但最终看起来它不会显示出来!任何帮助,将不胜感激!

 <!DOCTYPE html> 
< body>
< div id =content>

< form name =calc>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js>< / script>

< script id =NumberButtons>
$(document).ready(function(){
$('#button1')。click(function(){
$('#output')。val($(this ).val());
));
$('#button2')。click(function(){
$('#output')。val($(this) .val());
});
$('#button3')。click(function(){
$('#output')。val($(this)。 val());
));
$('#button4')。click(function(){
$('#output')。val($(this).val ());
));
$('#button5')。click(function(){
$('#output')。val($(this).val ));
));
$('#button6')。click(function(){
$('#output')。val($(this).val() );
));
$('#button7')。click(function(){
$('#output')。val($(this).val()) ;
));
$('#button8')。click(function(){
$('#output')。val($(this).val()); $($#$ b));
$('#button9')。click(function(){
$('#output')。val($(this).val());
});
$('#button0')。click(function(){
$('#output')。val($(this).val());
});
$('#buttonclear')。click(function(){
$('#output')。val($(this).val());
});
});
< / script>
< script id =MathSymbolButtons>
var alpha = 0;
var bravo = 0;
var charlie = 0;
$ b $(document).ready(function(){
$('#buttonplus')。click(function(){
var alpha = $(#output ).val();
window.alert(alpha)
});
$('#buttonequals')。click(function(){
var bravo = $( #output)。val();
window.alert(bravo)
var charlie = alpha + bravo;
$('#output')。val($(charlie)。 val());
});
});
< / script>
< table>
< tr>
< td>
< input type =textid =output>
< br>
< / td>
< / tr>
< tr>
< td>
< input type =buttonname =onevalue =1id =button1>
< input type =buttonname =twovalue =2id =button2>
< input type =buttonname =threevalue =3id =button3>
< input type =buttonname =fourvalue =4id =button4>
< input type =buttonname =fivevalue =5id =button5>
< input type =buttonname =sixvalue =6id =button6>
< input type =buttonname =sevenvalue =7id =button7>
< input type =buttonname =eightvalue =8id =button8>
< input type =buttonname =ninevalue =9id =button9>
< input type =buttonname =zerovalue =0id =button0>
< input type =buttonname =addvalue =+id =buttonplus>
< input type =buttonname =evaluatevalue ==id =buttonequals>
< br>
< / td>
< / tr>
< / table>
< / form>
< / div>
< / body>
< / html>


解决方案

这是您当前的代码:

  var alpha = 0; 
var bravo = 0;
var charlie = 0;
$ b $('#buttonplus')。click(function(){
var alpha = $(#output)。val();
});
$ b $('#buttonequals')。click(function(){
var bravo = $(#output)。val();
var charlie = alpha + bravo;
$('#output')。val($(charlie).val());
});

这些是全局变量:

  var alpha = 0; 
var bravo = 0;
var charlie = 0;

但是您正在中创建新的局部变量,请点击处理程序:

  $('#buttonplus')。click(function(){
var alpha = $(#output)。val();
});

移除 var 来存取全局变量:

  $('#buttonplus')。click(function(){
alpha = $(#output ).val();
});



此代码将字符串分配给 #output to alpha:

  alpha = $(#output ).VAL(); 

如果 alpha 是2并且 bravo 是3,那么 alpha + bravo 会是 23 p>

您可以通过加一个加号将它强制为数字:

  alpha = + $(#output)。val(); 



最后,而不是这个:

  $('#output')。val($(charlie).val()); 

…这样做:

  $('#output')。val(charlie); 






更正后的代码:

  var alpha = 0; 
var bravo = 0;
var charlie = 0;

$('#buttonplus')。click(function(){
alpha = + $(#output)。val();
});

'('#buttonequals')。click(function(){
bravo = + $(#output)。val();
charlie = alpha + bravo ;
$('#output')。val(charlie);
});


i am having a little trouble here. im building a calculator and im working on the general idea on it and im confused on why my variable charlie will not appear in the output text box. All the variables are correct in the end ( i checked them with the alert function) but in the end it seems that it just wont show up! any help would be appreciated!

<!DOCTYPE html>
<body>
<div id="content">

<form name="calc"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script id="NumberButtons">
  $(document).ready(function(){
   $('#button1').click(function(){
        $('#output').val($(this).val());
    });
   $('#button2').click(function(){
        $('#output').val($(this).val());
    });
   $('#button3').click(function(){
        $('#output').val($(this).val());
    });
   $('#button4').click(function(){
        $('#output').val($(this).val());
    });
   $('#button5').click(function(){
        $('#output').val($(this).val());
    });
   $('#button6').click(function(){
        $('#output').val($(this).val());
    });
   $('#button7').click(function(){
        $('#output').val($(this).val());
    });
   $('#button8').click(function(){
        $('#output').val($(this).val());
    });
   $('#button9').click(function(){
        $('#output').val($(this).val());
    });
   $('#button0').click(function(){
        $('#output').val($(this).val());
    });
   $('#buttonclear').click(function(){
        $('#output').val($(this).val());
    });
  });
</script>
<script id="MathSymbolButtons">
var alpha = 0;
var bravo = 0;
var charlie = 0;

  $(document).ready(function(){
        $('#buttonplus').click(function(){
             var alpha = $("#output").val();
             window.alert(alpha)
             });
         $('#buttonequals').click(function(){
              var bravo = $("#output").val();
              window.alert(bravo)
              var charlie = alpha + bravo;
              $('#output').val($(charlie).val());
              });
  });
</script>
 <table>
    <tr>
         <td>
         <input type="text"   id="output">
        <br>
        </td>
        </tr>
    <tr>
        <td>
        <input type="button" name="one" value = "1" id="button1"> 
        <input type = "button" name = "two" value = "2" id="button2" > 
        <input type = "button" name = "three" value = "3" id="button3"> 
        <input type = "button" name = "four" value = "4"id="button4"> 
        <input type = "button" name = "five" value = "5" id="button5"> 
        <input type = "button" name = "six" value = "6" id="button6"> 
        <input type = "button" name = "seven" value = "7" id="button7"> 
        <input type = "button" name = "eight" value = "8" id="button8"> 
        <input type = "button" name = "nine" value = "9" id="button9"> 
        <input type = "button" name = "zero" value = "0" id="button0"> 
        <input type = "button" name = "add" value = "+" id="buttonplus">
        <input type = "button" name = "evaluate" value = " = "     id="buttonequals"> 
        <br>
        </td>
    </tr>
</table>
</form>
</div>
</body>
</html>

解决方案

Here is your current code:

var alpha = 0;
var bravo = 0;
var charlie = 0;

$('#buttonplus').click(function(){
  var alpha = $("#output").val();
});

$('#buttonequals').click(function(){
  var bravo = $("#output").val();
  var charlie = alpha + bravo;
  $('#output').val($(charlie).val());
});

These are global variables:

var alpha = 0;
var bravo = 0;
var charlie = 0;

But you're creating new local variables within the click handlers:

$('#buttonplus').click(function(){
  var alpha = $("#output").val();
});

Remove var to access the global variables instead:

$('#buttonplus').click(function(){
  alpha = $("#output").val();
});


This code assigns the string representation of #output to alpha:

alpha = $("#output").val();

If alpha is 2 and bravo is 3, then alpha + bravo would be 23.

You can coerce it to a number by prepending a plus sign:

alpha = +$("#output").val();


Finally, instead of this:

  $('#output').val($(charlie).val());

… do this:

  $('#output').val(charlie);


Corrected code:

var alpha = 0;
var bravo = 0;
var charlie = 0;

$('#buttonplus').click(function(){
  alpha = +$("#output").val();
});

$('#buttonequals').click(function(){
  bravo = +$("#output").val();
  charlie = alpha + bravo;
  $('#output').val(charlie);
});

这篇关于为什么我的变量不会出现在使用Jquery的TextBox中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆