Я хочу удалить onblur и имя (this.form) в документе HTML, поэтому, когда пользователь нажимает кнопку, он просто вызывает функцию - PullRequest
0 голосов
/ 10 мая 2011
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Untitled Document</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta http-equiv="Content-Style-Type" content="text/css">
        <meta http-equiv="Content-Script-Type" content="text/javascript">
        <script type="text/javascript">
            //Create a global an global awaay for strCurrency & dblExchange Rate
            var strCurrency=['Yen','US Dollar','Euro','Swiss Frank','Danish Korona'];
            var dblExchangeRate=[128.7, 1.59, 1.15, 2.06, 9.69];

            //Declare the function convert currency 
            function convertCurrency()
            {
                var txtSterling= document.getElementById("txtSterling");
                var selCurrency= document.getElementById("cmbCurrency");
                var txtConversion= document.getElementById ("txtConversion");

                //reset to no value
                txtConversion.value='';

                //validate for numbers
                if(isNaN(txtSterling.value))
                {
                    txtSterling.value='';
                    alert('Please insert a numerical value');
                    txtSterling.focus();
                    return;
                }

                var strCurrency=selCurrency = document.getElementById("cmbCurrency").selectedIndex;
                txtConversion.value = (txtSterling.value * dblExchangeRate[i]).toFixed(2) + ' ' + strCurrency[i];
            }
        </script>
    </head>
    <body>
        <p>Currency Converter</p>
        <form action="">
            &pound; <input type="text" name="txtSterling" onblur="convertCurrency(this.form)">
            <select name="cmbCurrency" onchange="convertCurrency(this.form)">
                <option>Yen</option>
                <option>US Dollars</option>
                <option>Euro</option>
                <option>Swiss Frank</option>
                <option>Danish Korona</option>
            </select>
            <input type="button" value="Convert" name="cmdCurrency" onclick="convertCurrency(this.form)">
            <input type="text" name="txtConversion" readonly="readonly">
        </form>
    </body>
</html>

1 Ответ

0 голосов
/ 10 мая 2011

Поскольку ваша функция convertCurrency не требует никаких аргументов, вам даже не нужен параметр this.form. Также вы можете просто удалить blur = ..., чтобы избавиться от события onblur. То же самое относится и к событию onchange для select, если вы тоже хотите от него избавиться.

ОБНОВЛЕНО: Глупый я за копирование ...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Untitled Document</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta http-equiv="Content-Style-Type" content="text/css">
        <meta http-equiv="Content-Script-Type" content="text/javascript">
        <script type="text/javascript">
            //Create a global an global awaay for strCurrency & dblExchange Rate
            var strCurrency=['Yen','US Dollar','Euro','Swiss Frank','Danish Korona'];
            var dblExchangeRate=[128.7, 1.59, 1.15, 2.06, 9.69];

            //Declare the function convert currency 
            function convertCurrency()
            {
                var txtSterling= document.getElementById("txtSterling");
                var selCurrency= document.getElementById("cmbCurrency");
                var txtConversion= document.getElementById ("txtConversion");
                var cmbCurrency = document.getElementById("cmbCurrency");

                //reset to no value
                txtConversion.value='';

                //validate for numbers
                if(isNaN(txtSterling.value))
                {
                    txtSterling.value='';
                    alert('Please insert a numerical value');
                    txtSterling.focus();
                    return;
                }

                var i = cmbCurrency.selectedIndex;
                var strCurrency = cmbCurrency.options[i].text;
                txtConversion.value = (txtSterling.value * dblExchangeRate[i]).toFixed(2) + ' ' + strCurrency;
            }
        </script>
    </head>
    <body>
        <p>Currency Converter</p>
        <form action="">
            &pound; <input id="txtSterling" type="text" name="txtSterling">
            <select id="cmbCurrency" name="cmbCurrency" onchange="convertCurrency();">
                <option>Yen</option>
                <option>US Dollars</option>
                <option>Euro</option>
                <option>Swiss Frank</option>
                <option>Danish Korona</option>
            </select>
            <input type="button" id="cmdCurrency" value="Convert" name="cmdCurrency" onclick="convertCurrency();">
            <input type="text" id="txtConversion" name="txtConversion" readonly="readonly">
        </form>
    </body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...