简体   繁体   中英

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.

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.

You're doing it way wrong.

  1. With if statements, you use == instead of =.

     = 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.

     $( document ).ready(function() { //taken from api.jquery.com/ready/ }); 
  3. If you're using jQuery, use # when refering to ID objects, ie.

     $('#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()".

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. You can change this to something like $("btn-submit-order") . Same thing to reset.

Moreover, when you test $("tax").value = 0 I think you mistyped = instead of == .

Other issues...

I think you mean

if ($("#tax").val() == 0)

Note:

  • Uses the correct selector #
  • Uses the jQuery val() function. The jQuery object doesn't have a value property.
  • Compares to 0 using loose checking, though personally I would write the line as
    • if (+$("#tax").val() === 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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