简体   繁体   English

不响应JS功能的命令按钮

[英]Command Buttons Not Responding to JS functions

I am very close to finishing this program but am unable to get past one last hurdle. 我非常接近完成这个程序,但是无法克服最后的障碍。 I want some very simple code to execute when the command buttons are pressed. 我希望在按下命令按钮时执行一些非常简单的代码。 When the Submit Order button is pressed the following code should run to check that the form is completed. 当按下Submit Order按钮时,应运行以下代码来检查表单是否完成。

function validateForm()
{
    if ($("tax").value = 0)
    {
        alert ("You have not selected anything to order");  
    }

    if ($("shipCost").value = 0)
    {
        alert("You must select a method of shipping");
    }
}

And when the reset button is pressed the following code should run. 按下复位按钮后,应运行以下代码。

function initForm() 
{
    $('date').value = todayTxt();
    $('qty1').focus();
}

Unfortunately the buttons are not executing the code which I am trying to execute through the following set of functions. 不幸的是,这些按钮没有执行我试图通过以下一组功能执行的代码。

window.onload = function ()
{
    initForm();
    todayTxt();
    productCosts();
    shipExpense();
    $('shipping').onchange = calcShipping;
    calcShipping();
    $("Submit Order").onclick = validateForm();
    $("reset").onclick = initForm();
}

I have created a fiddle so you can see the full program: http://jsfiddle.net/KhfQ2/ Any help is greatly appreciated. 我创建了一个小提琴,以便您可以看到完整的程序: http : //jsfiddle.net/KhfQ2/任何帮助都将不胜感激。

You're doing it way wrong. 您做错了。

  1. With if statements, you use == instead of =. 对于if语句,可以使用==代替=。

     = in A = B means assign value of B to A == in A == B means A equals B 
  2. Read about .ready and use it instead of window.onLoad, it's quite a bad choice when it comes to binding, ie. 阅读有关.ready的信息,并使用它代替window.onLoad进行绑定时,这是一个不好的选择,即。

     $( document ).ready(function() { //taken from api.jquery.com/ready/ }); 
  3. If you're using jQuery, use # when refering to ID objects, ie. 如果您使用的是jQuery,则在引用ID对象时即使用#。

     $('#tax').val(); 
  4. On no account should you use spaces when giving any object a unique name or class! 给任何对象唯一的名称或类时,绝对不要使用空格!

  5. Pay attention to letters. 注意字母。 You had ".clisk()" instead of "click()". 您使用的是“ .clisk()”而不是“ click()”。

Check it out and provide us with fixed code. 检查一下,并向我们提供固定的代码。

It is simple. 很简单。 $("Submit Order") doesn't work, because the button doesn't have this id. $("Submit Order")不起作用,因为按钮没有此ID。 You can change this to something like $("btn-submit-order") . 您可以将其更改为$("btn-submit-order") Same thing to reset. 重置同样的事情。

Moreover, when you test $("tax").value = 0 I think you mistyped = instead of == . 此外,当您测试$("tax").value = 0我认为您输入了=而不是==

Other issues... 其他事宜...

I think you mean 我想你是说

if ($("#tax").val() == 0) 如果($(“#tax”)。val()== 0)

Note: 注意:

  • Uses the correct selector # 使用正确的选择#
  • Uses the jQuery val() function. 使用jQuery val()函数。 The jQuery object doesn't have a value property. jQuery对象没有value属性。
  • Compares to 0 using loose checking, though personally I would write the line as 使用松散检查与0进行比较,尽管我个人会将其写为
    • if (+$("#tax").val() === 0) 如果(+ $(“#tax”)。val()=== 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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