【问题标题】:OnChange event not working on select boxOnChange 事件在选择框上不起作用
【发布时间】:2014-06-11 23:46:30
【问题描述】:

以下代码产生 JavaScript 错误: Uncaught TypeError: undefined is not a function: 订货量 改变

据我所知,这个脚本应该可以工作。我已经用谷歌搜索了这个问题,所有的建议都涉及重命名事物,因此它们不会发生冲突,但我确保我没有冲突的元素名称或 ID。

这可能是印刷问题,但我找不到问题。请帮忙。

<script type="text/javascript">

function orderquantity(myvalue)
    {
        if (myvalue == "Custom") {
            document.getElementByID('cquant').style.display="inline-block";
        }else{
            document.getElementByID('cquant').style.display="none";
            document.getElementByID('customquant').value="";
        }
    }
</script>

<select name="quantity" id="quant" onchange="orderquantity(this.selectedIndex);">
    <option value="MatchLast" selected="selected">Same as Last Time</option>
    <option value="500">500</option>
    <option value="1000">1000</option>
    <option value="2500">2500</option>
    <option value="5000">5000</option>
    <option value="10000">10000</option>
    <option value="Custom">Custom Quantity</option>
</select>

<div id="cquant" style="display: none;">
    <input type="text" name="CustomAmount" id="customquant">
</div>

【问题讨论】:

    标签: javascript html forms onchange


    【解决方案1】:

    使用this.value 代替this.selectedIndex。另外:getElementByID 在您的代码中应该是 getElementById

    Example

    【讨论】:

      【解决方案2】:

      同意,这很可能是您 getElementByID 的问题。您正在寻找的是 getElementById。

      正如 Nolutysses 所说,您应该使用 this.value。但是您也可以保留除了添加 .innerHTML 之外的所有内容。所以应该是 this.selectedIndex.innerHTML。但是this.value更短更容易理解

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-22
        • 1970-01-01
        • 1970-01-01
        • 2022-06-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多