Set the value of an input field

前端 未结 15 1527
故里飘歌
故里飘歌 2020-11-22 00:54

How would you set the default value of a form text field in JavaScript?

相关标签:
15条回答
  • 2020-11-22 01:52

    This part you use in html

    <input id="latitude" type="text" name="latitude"></p>
    

    This is javaScript:

    <script>
    document.getElementById("latitude").value=25;
    </script>
    
    0 讨论(0)
  • 2020-11-22 01:53
    document.getElementById("fieldId").value = "Value";
    

    or

    document.forms['formId']['fieldId'].value = "Value";
    

    or

    document.getElementById("fieldId").setAttribute('value','Value');
    
    0 讨论(0)
  • 2020-11-22 01:53
    <form>
      <input type="number"  id="inputid"  value="2000" />
    </form>
    
    
    <script>
    var form_value = document.getElementById("inputid").value;
    </script>    
    

    You can also change the default value to a new value

    <script>
    document.getElementById("inputid").value = 4000;     
    </script>
    
    0 讨论(0)
提交回复
热议问题