简体   繁体   English

jQuery 点击事件似乎不起作用

[英]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">&#247;</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:在原始代码中包含一些错误的语法:

  1. 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 () 的用法在代码中是错误的......

  2. 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">&#247;</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()

  1. Add a common className (eg 'numberKey') to your calculator buttons representing numbers.向代表数字的计算器按钮添加一个通用的类名(例如“numberKey”)。

    <button type="button" class="numberKey" value="1">1</button>

  2. 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}`); }


FYI, the jQuery .append is not a property that can be appended to, or set to a value. 仅供参考,jQuery .append不是可以附加到或设置为值的属性。 It's a function which can be passed a string value. 这是一个 function 可以传递一个字符串值。 And that value will be appended to the contents of the selected element(s). 并且该值将附加到所选元素的内容中。

If you want to clear an input element, you should set its value to a blank string: 如果要清除输入元素,应将其值设置为空白字符串:
 $('#display').val('');


The 'ans' (answer?) function can work like this: “ans”(答案?) function 可以这样工作:
 function solve_ans() { let expression = $('#display').val(); let solution = eval(expression); $('#display').val(solution); }


Also, note that if you are using the same selector more than once in any context, it's usually worthwhile to cache/store the result of the selection in a variable and then reuse it, rather than re-selecting the same element(s). 另外,请注意,如果您在任何上下文中多次使用相同的选择器,通常值得将选择的结果缓存/存储在变量中然后重用它,而不是重新选择相同的元素。
 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM