-3

I have this form in which, when the order type selected is a market order the stop price and limit price should have value zero and should be readonly.

当我选择限价单时,它应该使止损价格为 0 并且只读。

当我选择止损单时,它应该将限价设为 0 并且只读。

我试图在 js 中使用简单的 if-else。我究竟做错了什么?

请尽量避免使用 jquery。请教学校项目。提前致谢...

我在下面有我的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<link rel="stylesheet" type="text/css" href="ETcss.css" />
<script src="jquery-1.10.2.min.js"></script>
</head>

<body>

    <div class="header">
        <h1 style="font-family: Cambria; color: #F0F0F0;" align="center">Welcome User</h1>

        <br>
    </div>


    <div class="sidemenu">
        <ul>
            <li>
                <a href="pmHome.html">Home</a>
            </li>
            <li>
                <a href="createOrder.html">Create Order</a>
            </li>

        </ul>
    </div>
    <div class="mainmenu">
        <h1>Create Equity Order</h1>
        <form action="">
            <table>

                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Symbol :</td>
                    <td>
                        <input type="text" name="symbol" />
                    </td>
                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Secutrity name :</td>
                    <td>
                        <input type="text" name="securityName" value="sapient" placeholder="sapient" />
                    </td>
                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Side :</td>
                    <td>
                        <select>
                            <option>buy</option>
                            <option>sell</option>
                        </select>
                    </td>
                </tr>

                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Order Type :</td>
                    <td>
                        <select id="orderType">
                            <option value="market" selected>market</option>
                            <option value="limit">limit</option>
                            <option value="stop">stop</option>
                        </select>
                    </td>
                </tr>

                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Order Qualifiers :</td>
                    <td>
                        <select>
                            <option>day order</option>
                            <option>gtc</option>
                        </select>
                    </td>
                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Trader :</td>
                    <td>
                        <select>
                            <option>trader1</option>
                            <option>trader2</option>
                        </select>
                    </td>

                </tr>

                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Account Type :</td>
                    <td>
                        <select>
                            <option>cash</option>
                            <option>margin</option>
                        </select>
                    </td>

                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Portfolio :</td>
                    <td>
                        <select>
                            <option>p1</option>
                            <option>p2</option>
                        </select>
                    </td>

                </tr>

                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">

                    <td>Stop Price :</td>
                    <td>
                        <input type="text" id="stopprice" name="stopprice" readonly="readonly" onfocus="this.blur()" />
                    </td>

                </tr>

                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">

                    <td>Limit Price :</td>
                    <td>
                        <input type="text" id="limitprice" name="limitprice" readonly="readonly" onfocus="this.blur()" />
                    </td>

                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">


                    <td>Qty :</td>
                    <td>
                        <input type="text" name="qty" />
                    </td>
                </tr>


                <tr>
                    <a href="pmHome.html">
                        <td colspan="2" align="center">
                            <input type="submit" onclick="display_alert()" value="Create Equity Order" />
                    </a>
                    </td>
                </tr>

            </table>
        </form>
    </div>
</body>
<script>
function updatePrice(el, priceLog, priceLog1, priceList) {
    var e = document.getElementById("orderType");
    var pricevalue = e.options[e.selectedIndex].text;
    if (pricevalue.toLowerCase() == "market") {
        priceLog.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value
                .toLowerCase()];
        priceLog1.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value
                .toLowerCase()];
    }
    alert(pricevalue);
    else if (pricevalue.toLowerCase() == "limit") {
        document.getElementByName('limitprice').readOnly = false;
        priceLog.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value
                .toLowerCase()];
        priceLog1.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value
                .toLowerCase()];

    } else if (pricevalue.toLowerCase() == "stop") {
        document.getElementByName('stopprice').readOnly = false;
        priceLog.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value
                .toLowerCase()];
        priceLog1.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value
                .toLowerCase()];

    }
}

var stopPrices = {
    'market' : 0,
    'limit' : 1,
    'stop' : 2,
};
var limitPrices = {
    'market' : 0,
    'limit' : 1,
    'stop' : 2,
};

var select = document.getElementById('orderType'), hidden = document
        .getElementsByName('stopprice')[0];
hidden1 = document.getElementsByName('limitprice')[0];

select.addEventListener('change', function() {
    updatePrice(select, hidden, hidden1, stopPrices);
});

 </script>
</html>
4

2 回答 2

1

问题

  1. 之间ifelse你放了一个alert()- SYNTAX ERROR
  2. el, e, 和select都相同并且具有值 - document.getElementById('orderType')
  3. onfocus="this.blur()"!!!,我停在这里。

希望这段代码能解决您的问题 -

<html>
<head>
<link rel="stylesheet" type="text/css" href="ETcss.css" />
<script src="jquery-1.10.2.min.js"></script>
</head>

<body>

    <div class="header">
        <h1 style="font-family: Cambria; color: #F0F0F0;" align="center">Welcome User</h1>
        <br>
    </div>
    <div class="sidemenu">
        <ul>
            <li>
                <a href="pmHome.html">Home</a>
            </li>
            <li>
                <a href="createOrder.html">Create Order</a>
            </li>

        </ul>
    </div>
    <div class="mainmenu">
        <h1>Create Equity Order</h1>
        <form action="">
            <table>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Symbol :</td>
                    <td>
                        <input type="text" name="symbol" />
                    </td>
                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Secutrity name :</td>
                    <td>
                        <input type="text" name="securityName" value="sapient" placeholder="sapient" />
                    </td>
                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Side :</td>
                    <td>
                        <select>
                            <option>buy</option>
                            <option>sell</option>
                        </select>
                    </td>
                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Order Type :</td>
                    <td>
                        <select id="orderType">
                            <option value="market" selected>market</option>
                            <option value="limit">limit</option>
                            <option value="stop">stop</option>
                        </select>
                    </td>
                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Order Qualifiers :</td>
                    <td>
                        <select>
                            <option>day order</option>
                            <option>gtc</option>
                        </select>
                    </td>
                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Trader :</td>
                    <td>
                        <select>
                            <option>trader1</option>
                            <option>trader2</option>
                        </select>
                    </td>

                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Account Type :</td>
                    <td>
                        <select>
                            <option>cash</option>
                            <option>margin</option>
                        </select>
                    </td>

                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Portfolio :</td>
                    <td>
                        <select>
                            <option>p1</option>
                            <option>p2</option>
                        </select>
                    </td>

                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">

                    <td>Stop Price :</td>
                    <td>
                        <input type="text" id="stopprice" name="stopprice" readonly="readonly"  />
                    </td>

                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">

                    <td>Limit Price :</td>
                    <td>
                        <input type="text" id="limitprice" name="limitprice" readonly="readonly"  />
                    </td>
                </tr>
                <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td>Qty :</td>
                    <td>
                        <input type="text" name="qty" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" onclick="display_alert()" value="Create Equity Order" />
                    </td>
                </tr>
            </table>
        </form>
    </div>
</body>
<script>
    var select = document.getElementById('orderType');
    var priceLog = document.getElementsByName('stopprice')[0];
    var priceLog1 = document.getElementsByName('limitprice')[0];

    var stopPrices = {
        'market' : 0,
        'limit' : 1,
        'stop' : 2,
    };
    var limitPrices = {
        'market' : 0,
        'limit' : 1,
        'stop' : 2,
    };

    select.addEventListener('change', updatePrice);

    function updatePrice() {
        var select = document.getElementById("orderType");
        var selectedIndex = select.selectedIndex;

        var pricevalue = select.options[selectedIndex].text.toLowerCase();
        var selectedValue = select.options[selectedIndex].value.toLowerCase();

        if (pricevalue == "market") {
            priceLog.disabled = "disabled";
            priceLog1.disabled = "disabled";
            priceLog.value = 0;
            priceLog1.value = 0;

        } else if (pricevalue == "limit") {
            priceLog1.disabled = "disabled";
            priceLog.removeAttribute("disabled");
            priceLog1.value = 0;

        } else if (pricevalue == "stop") {
            priceLog.disabled = "disabled";
            priceLog1.removeAttribute("disabled");
            priceLog.value = 0;
        }
    }
</script>
</html>
于 2013-09-13T10:44:17.900 回答
0

Moazzam Khan 正确地说,有一个语法警报以及没有参数:

select.addEventListener('change', function(){ updatePrice(document.getElementById('orderType'), document.getElementsByName('stopprice')[0], document.getElementsByName('limitprice')[0], stopPrices); },错误的);

使用以下脚本代码:

<script>
function updatePrice (el, priceLog, priceLog1, priceList) {
try{
 var e = document.getElementById("orderType");
 var pricevalue = e.options[e.selectedIndex].text;
if(pricevalue.toLowerCase() == "market"){
    priceLog.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value.toLowerCase()];
    priceLog1.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value.toLowerCase()];
    }
else if(pricevalue.toLowerCase() == "limit"){
    document.getElementByName('limitprice').readOnly = false;
    priceLog.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value.toLowerCase()];
    priceLog1.value =priceList[el.getElementsByTagName('option')[el.selectedIndex].value.toLowerCase()];
}
else if(pricevalue.toLowerCase() == "stop"){
    document.getElementByName('stopprice').readOnly = false;
    priceLog.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value.toLowerCase()];
    priceLog1.value = priceList[el.getElementsByTagName('option')[el.selectedIndex].value.toLowerCase()];
}
}
catch(err)
{
}
}

var stopPrices = {
'market' : 0,
'limit' : 1,
'stop' : 2,
 };
 var limitPrices={
 'market' : 0,
'limit' : 1,
 'stop' : 2,
  };

 var select = document.getElementById('orderType'),
 hidden = document.getElementsByName('stopprice')[0];
hidden1= document.getElementsByName('limitprice')[0];

select.addEventListener('change', function(){
updatePrice(document.getElementById('orderType'), document.getElementsByName('stopprice')[0], document.getElementsByName('limitprice')[0], stopPrices); 
},false);


 </script>
于 2013-09-13T11:22:25.857 回答