如何使用JavaScript在输入字段中输入值?

时间:2018-07-13 13:01:09

标签: javascript

我正在尝试使用javascript将一些文本添加到输入搜索字段

我尝试了以下代码:

document.getElementsByClassName('contacts_modal_search_field')[0].innerHTML = \"test\

和其他许多选项,但未成功。

这是代码:

<div class="contacts_modal_search">
  <input class="form-control contacts_modal_search_field no_outline ng-pristine ng-valid ng-empty ng-touched" my-focused="" type="search" placeholder="Search" ng-model="search.query" autocomplete="off" style="">
  <a class="im_dialogs_search_clear tg_search_clear ng-hide" ng-click="search.query = ''" ng-show="search.query.length" style="">
    <i class="icon icon-search-clear"></i>
  </a>
</div>

3 个答案:

答案 0 :(得分:2)

使用document.getElementById("id").value = "yourvalue"document.getElementsByClassName("yourclass")[index].value = "yourvalue"

使用id

Enter text to be the value of the second input field:<br/>
<input type="text" id="text"/>
<br/>
<input type="button" onClick="changeValue()" value="Change Value"/>
<p/>
<input type="text" id="result"/>
<script>
function changeValue(){
 document.getElementById("result").value = document.getElementById("text").value;
}
</script>

使用class

Enter text to be the value of the second input field:<br/>
    <input type="text" class="text"/>
    <br/>
    <input type="button" onClick="changeValue()" value="Change Value"/>
    <p/>
    <input type="text" class="result"/>
    <script>
    function changeValue(){
     document.getElementsByClassName("result")[0].value = document.getElementsByClassName("text")[0].value;
    }
    </script>

输入您的代码

Enter text to be the value of the search input field:<br/>
    <input type="text" class="text"/>
    <br/>
    <input type="button" onClick="changeValue()" value="Change Value"/>
    <p/>
    <script>
    function changeValue(){
     document.getElementsByClassName("form-control")[0].value = document.getElementsByClassName("text")[0].value;
    }
    </script>
<hr>
<div class="contacts_modal_search">
  <input class="form-control contacts_modal_search_field no_outline ng-pristine ng-valid ng-empty ng-touched" my-focused="" type="search" placeholder="Search" ng-model="search.query" autocomplete="off" style="">
  <a class="im_dialogs_search_clear tg_search_clear ng-hide" ng-click="search.query = ''" ng-show="search.query.length" style="">
    <i class="icon icon-search-clear"></i>
  </a>
</div>

在窗口加载时更改输入框的值(使用window.onload):

        <script>
        window.onload = function(){
         document.getElementsByClassName("form-control")[0].value = "your value";
        }
        </script>
    <div class="contacts_modal_search">
      <input class="form-control contacts_modal_search_field no_outline ng-pristine ng-valid ng-empty ng-touched" my-focused="" type="search" placeholder="" ng-model="search.query" autocomplete="off" style="">
      <a class="im_dialogs_search_clear tg_search_clear ng-hide" ng-click="search.query = ''" ng-show="search.query.length" style="">
        <i class="icon icon-search-clear"></i>
      </a>
    </div>

答案 1 :(得分:0)

也许是这样的:

document.getElementsByClassName('contacts_modal_search_field')[0].value = "test";
 <div class="contacts_modal_search">
  <input class="form-control contacts_modal_search_field no_outline ng-pristine ng-valid ng-empty ng-touched" my-focused="" type="search" placeholder="Search" ng-model="search.query" autocomplete="off" style="">
  <a class="im_dialogs_search_clear tg_search_clear ng-hide" ng-click="search.query = ''" ng-show="search.query.length" style="">
    <i class="icon icon-search-clear"></i>
  </a>
</div>

答案 2 :(得分:0)

您可以使用Java脚本在文本框中输入。

class GameScene: SKScene, SKPhysicsContactDelegate {
    let avatar = SKShapeNode(circleOfRadius: 20)

    let avatarCategory: UInt32 = 0*1 << 0
    let obstacleCategory: UInt32 = 0*1 << 1

    override func didMove(to view: SKView) {
        physicsWorld.contactDelegate = self

        createAvatar()
        spawnObstacles()
    }

    func createAvatar() {
        avatar.name = "avatarNode"
        avatar.physicsBody = SKPhysicsBody()
        avatar.physicsBody?.categoryBitMask = avatarCategory
        avatar.physicsBody?.contactTestBitMask = avatarCategory
        avatar.physicsBody?.collisionBitMask = 0
        avatar.physicsBody?.usesPreciseCollisionDetection = true
        avatar.physicsBody?.affectedByGravity = false
        avatar.zPosition = 2

        addChild(avatar)
    }

    func createRandomObstacle() {
        let obstacle = SKShapeNode()

        obstacle.name = "obstacleNode"
        obstacle.physicsBody = SKPhysicsBody()
        obstacle.physicsBody?.categoryBitMask = obstacleCategory
        obstacle.physicsBody?.contactTestBitMask = obstacleCategory
        obstacle.physicsBody?.collisionBitMask = 0
        obstacle.physicsBody?.usesPreciseCollisionDetection = true
        obstacle.physicsBody?.affectedByGravity = false
        obstacle.zPosition = 2

        addChild(obstacle)
    }

    func didBegin(_ contact: SKPhysicsContact) {
        print("collision")
    }