Set the value of an input field

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

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

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

    Try out these.

    document.getElementById("current").value = 12
    
    // or
    
    var current = document.getElementById("current");
    current.value = 12
    
    0 讨论(0)
  • 2020-11-22 01:28

    This is one way of doing it:

    document.getElementById("mytext").value = "My value";
    
    0 讨论(0)
  • 2020-11-22 01:36

    I use 'setAttribute' function:

    <input type="text" id="example"> // Setup text field 
    <script type="text/javascript"> 
      document.getElementById("example").setAttribute('value','My default value');
    </script>
    
    0 讨论(0)
  • 2020-11-22 01:38

    The answer is really simple

    // Your HTML text field
    
    <input type="text" name="name" id="txt">
    
    //Your javascript
    
    <script type="text/javascript"> 
    document.getElementById("txt").value = "My default value";
    </script>
    

    Or if you want to avoid JavaScript entirely: You can define it just using HTML

    <input type="text" name="name" id="txt" value="My default value">
    
    0 讨论(0)
  • 2020-11-22 01:40

    It's simple; An example is:

    <input type="text" id="example"> // Setup text field 
    <script type="text/javascript"> 
    var elem = document.getElementById("example"); // Get text field
    elem.value = "My default value"; // Change field
    </script>
    
    0 讨论(0)
  • 2020-11-22 01:42

    The simple answer is not in Javascript the simplest way to get the placeholder is through the place holder attribute

    <input type="text" name="text_box_1" placeholder="My Default Value" />
    
    0 讨论(0)
提交回复
热议问题