【问题标题】:How to clear text field automatically when enter minus value输入负值时如何自动清除文本字段
【发布时间】:2022-12-18 13:36:27
【问题描述】:

`

<input type="number" id="qty">
<input type="button" onclick="add()" value="Add">

<script>
  function add(){
    qty = document.getElementById('qty').value
    if(qty>0){
        var tot = qty*25
        document.write(tot)
    
    }else{
        qty = ""
        alert("no")
    }
  }
</script>

'qty=""' 是谷歌上提到的正确方法。但不为我工作。

【问题讨论】:

  • document.getElementById("数量").value = "";

标签: javascript


【解决方案1】:

尝试

document.getElementById("qty").value = "";

【讨论】:

    【解决方案2】:

    输入的值总是string,在你的情况下试试这个:

    if(Number(qty)>0){
    ...
    }
    

    【讨论】:

      【解决方案3】:

      使用下面的代码清除输入

      qty.value = "";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多