[英]jQuery click event doesn't seem to be working
I'm practicing doing a simple calculator using jQuery, I feel like the code is pretty simple in its own but no matter what I do absolutely nothing happens, as if there was no js file linked to beging with.我正在练习使用 jQuery 做一个简单的计算器,我觉得代码本身很简单,但无论我做什么都没有任何反应,好像没有链接到 beging 的 js 文件。 Here's the jQuery code:
这是 jQuery 代码:
function ud(n){
$("#display").append += n;
}
function ans(c){
c = eval($("#display").html);
$("#display").append = c;
}
function clc(){
$("#display").append = '';
}
$("#button0").click(ud(0))
$("#button1").click(ud(1));
$("#button2").click(ud(2));
$("#button3").click(ud(3));
$("#button4").click(ud(4));
$("#button5").click(ud(5));
$("#button6").click(ud(6));
$("#button7").click(ud(7));
$("#button8").click(ud(8));
$("#button9").click(ud(9));
$("#addButton").click(ud('+'));
$("#subtractButton").click(ud('-'));
$("#multiplyButton").click(ud('*'));
$("#clearButton").click(clc());
$("#equalsButton").click(ans());
$("#divideButton").click(ud('/'));
and here is the html:这是 html:
<tr>
<td colspan="4"><input id="display" name="display" disabled></input></td>
</tr>
<tr>
<td><button id="button1" value="1">1</button></td>
<td><button id="button2" value="2">2</button></td>
<td><button id="button3" value="3">3</button></td>
<td><button id="addButton">+</button></td>
</tr>
<tr>
<td><button id="button4" value="4">4</button></td>
<td><button id="button5" value="5">5</button></td>
<td><button id="button6" value="6">6</button></td>
<td><button id="subtractButton">-</button></td>
</tr>
<tr>
<td><button id="button7" value="7">7</button></td>
<td><button id="button8" value="8">8</button></td>
<td><button id="button9" value="9">9</button></td>
<td><button id="multiplyButton">*</button></td>
</tr>
<tr>
<td><button id="clearButton">C</button></td>
<td><button id="button0" value="0">0</button></td>
<td><button id="equalsButton">=</button></td>
<td><button id="divideButton">÷</button></td>
</tr>
Could someone explain to me what I am doing wrong?有人可以向我解释我做错了什么吗? is there a much simpler way to do it?
有更简单的方法吗? thank you in advance
先感谢您
I modify your code and refactor.我修改您的代码并重构。 It should work.
它应该工作。
In the original code contains some wrong syntax:在原始代码中包含一些错误的语法:
input filed should set value, so you should use val()
, not use append()
, and the usage of append() is wrong in the code...输入字段应该设置值,所以你应该使用
val()
,而不是使用append()
,并且append () 的用法在代码中是错误的......
click event pass parameter, please see jQuery click-eventData-handler and pass an execuded function is not expected.单击事件传递参数,请参阅jQuery click-eventData-handler并传递执行的 function 是不期望的。
function ud(e) { var currentVal = $("#display").val(); $("#display").val(currentVal + e.data.n); } function ans(c) { var result = eval($("#display").val()); $("#display").val(result); } function clc() { $("#display").val(''); } for(var i = 0; i < 10; i++) { $('#button' + i).click({n: i}, ud); } $("#addButton").click({n: '+'}, ud); $("#subtractButton").click({n: '-'}, ud); $("#multiplyButton").click({n: '*'}, ud); $("#divideButton").click({n: '/'}, ud); $("#clearButton").click(clc); $("#equalsButton").click(ans);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <table> <tr> <td colspan="4"><input id="display" name="display" disabled></input></td> </tr> <tr> <td><button id="button1" value="1">1</button></td> <td><button id="button2" value="2">2</button></td> <td><button id="button3" value="3">3</button></td> <td><button id="addButton">+</button></td> </tr> <tr> <td><button id="button4" value="4">4</button></td> <td><button id="button5" value="5">5</button></td> <td><button id="button6" value="6">6</button></td> <td><button id="subtractButton">-</button></td> </tr> <tr> <td><button id="button7" value="7">7</button></td> <td><button id="button8" value="8">8</button></td> <td><button id="button9" value="9">9</button></td> <td><button id="multiplyButton">*</button></td> </tr> <tr> <td><button id="clearButton">C</button></td> <td><button id="button0" value="0">0</button></td> <td><button id="equalsButton">=</button></td> <td><button id="divideButton">÷</button></td> </tr> </table>
You are calling a function within the.click() event function, but it instead expects a function reference or an anonymous function as a parameter. You are calling a function within the.click() event function, but it instead expects a function reference or an anonymous function as a parameter.
$jQuery.on() $jQuery.on()
$jquery.click() $jquery.click()
Add a common className (eg 'numberKey') to your calculator buttons representing numbers.向代表数字的计算器按钮添加一个通用的类名(例如“numberKey”)。
<button type="button" class="numberKey" value="1">1</button>
Select all of these buttons at once and use a single click event handler for all of them. Select 同时使用所有这些按钮,并为所有这些按钮使用单击事件处理程序。
$(function() { $('button.numberKey').click(updateDisplay); }); function updateDisplay(clickEventObject) { // The button that was clicked can be referenced using // the 'this' keyword. // Variable-ize the button element into a jQuery object. let $button = $(this); // Get the 'value' attribute of the button let number = $button.attr('value'); // Get the displayed value. var expression = $("#display").val(); // Append the number to the display's value $("#display").val(`{$expression}{$number}`); }
$('#display').val('');
function solve_ans() { let expression = $('#display').val(); let solution = eval(expression); $('#display').val(solution); }
function solve_ans() { // Referring to this $display variable avoids repeatedly re-selecting the same elements from the DOM whenever you may need to refer to them. let $display = $('#display'); let expression = $display.val(); let solution = eval(expression); $display.val(solution); $display.addClass('solution').removeClass('expression'); }
Good luck!祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.