【问题标题】:how to give a standart or a user value with javascript如何使用 javascript 赋予标准或用户价值
【发布时间】:2018-08-11 10:00:05
【问题描述】:

当用户不输入值时,如何为我的input 字段提供0? 我应该用一个

if  {
} 
else {
}

【问题讨论】:

  • 只是 if{}else{} 不会削减它;-) 使用 EventListener,然后检查值
  • 请更好地解释确切你想要什么

标签: javascript html forms input


【解决方案1】:

在 javascript 中设置,然后通过 javascript 提交表单。我们现在以编程方式设置数字输入的值,然后以编程方式提交它。

function setZero() {
  const input = document.getElementById('number');
  
  if(!parseInt(input.value)){
      input.value = 0;
  }

   console.log(input.value);
    
    document.getElementById("form").submit();
}
<form id="form">
<input id="number" type="text" value="1">
<button type="button" onclick="setZero()">submit</button>
</form>

【讨论】:

    【解决方案2】:

    您可以使用以下sn-p检查并为文本框设置0。

    <script>
    
    function checkVal() {
      var txtVal=document.getElementById('txtBx').value;
      if(txtVal=="")
      {
          document.getElementById('txtBx').value=0;
      }
    
    }
    
    </script>
    
    <input id="txtBx" type="text">
    <button type="button" onclick="checkVal()">Button</button>
    </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      相关资源
      最近更新 更多