JavaScript onblur事件处理

时间:2016-02-11 15:41:51

标签: javascript html javascript-events

我目前正在学习JavaScript,并且在操作事件方面遇到了麻烦。我想添加一个事件,该事件将在HTML中的某些text字段中注册输入,操纵它们,并在alert框中显示结果。

以下是HTML代码:

    <form name="order" onSubmit="totalPrice()">
        <p><label><input type="text" name="apples" onblur="apples()"/> Apples</label></p>
        <p><label><input type="text" name="oranges" onblur="oranges()"/> Oranges</label></p>
        <p><label><input type="text" name="bananas" onblur="bananas()"/> Bananas</label></p>
        <p><input type="reset" value="Reset"/>
        <input type="submit" value="Submit Query"/></p>
    </form>

这是JavaScript:

    var total = 0;

    function apples ()
    {
        var a = document.order.apples.value;

        total += a * 0.75;
    }

    function oranges ()
    {
        var o = document.order.oranges.value;

        total += o * 0.60;
    }

    function bananas ()
    {
        var b = document.order.bananas.value;

        total += b * 0.50;
    }

    function totalPrice ()
    {
        window.alert("Thank you for your order!\nYour total cost is: " + total);
    }

现在,如果我在apples()函数中调用oranges()bananas()totalPrice(),它可以解决问题,但来自input的调用标签似乎不起作用。

谢谢!

2 个答案:

答案 0 :(得分:9)

您必须以与功能

不同的方式命名输入

// Code goes here

  var total = 0;

    function apples_func ()
    {
        var a = document.order.apples.value;

        total += a * 0.75;
    }

    function oranges_func ()
    {
        var o = document.order.oranges.value;

        total += o * 0.60;
    }

    function bananas_func()
    {
        var b = document.order.bananas.value;

        total += b * 0.50;
    }

    function totalPrice ()
    {
     // apples();
      //oranges();
      //bananas();
        window.alert("Thank you for your order!\nYour total cost is: " + total);
    }
  <form name="order" onSubmit="totalPrice()">
        <p><label><input type="text" name="apples" onblur="apples_func()"/> Apples</label></p>
        <p><label><input type="text" name="oranges" onblur="oranges_func()"/> Oranges</label></p>
        <p><label><input type="text" name="bananas" onblur="bananas_func()"/> Bananas</label></p>
        <p><input type="reset" value="Reset"/>
        <input type="submit" value="Submit Query"/></p>
    </form>

答案 1 :(得分:0)

我认为你应该创建一个泛型函数并使用每个水果的name属性:

<form name="order" onSubmit="totalPrice()">
    <p><label><input type="text" name="apples" onblur="inputBlur(this)"/> Apples</label></p>
    <p><label><input type="text" name="oranges" onblur="inputBlur(this)"/> Oranges</label></p>
    <p><label><input type="text" name="bananas" onblur="inputBlur(this)"/> Bananas</label></p>
    <p><input type="reset" value="Reset"/>
    <input type="submit" value="Submit Query"/></p>
</form>

JS看起来像:

var fruitNumbers = {apple: 0, orange: 0, banana: 0};
var total = 0;
var prices = {apple: 0.75, orange: 0.6, banana: 0.5}

function inputBlur(fruitInput){
    var fruit = fruitInput.getAttribute('name');
    fruitNumbers[fruit] = this.value;
    calculateTotal();
}

function calculateTotal(){
    total = 0;
    for(fruit in fruitNumbers){
        total += fruitNumbers[fruit] * prices[fruit];
    }
}

function totalPrice ()
{
    window.alert("Thank you for your order!\nYour total cost is: " + total);
}