单击按钮时填充文本框的Javascript函数

时间:2016-01-12 09:39:15

标签: javascript php html

我想写一个javascript函数,以便填充从21,...,...的变量中取值的文本框。 值不可用的文本框(例如from11,from12,...等)应保持空白。实际上如果添加<ion-list> <ul class="list"> <li class="item" ng-repeat="ci in cityList | orderBy:'city' " ng-click="test.searchCity = ci.city">{{ci.city}} </li> </ul> </ion-list> ,那么这些框将被填充。但我希望盒子保持空载。这些只应在按钮点击时填写。

value='$from21'

1 个答案:

答案 0 :(得分:2)

jQuery会更容易。您可以将值存储在隐藏的输入中,然后单击填充所有输入 在这里存储:

<input type="hidden" id="value1" value="<?php echo $value1?>" name="value1">

填充在这里:

<input type="text" id="input1" value="" name="input1">

这里是javascript:

您有两种选择,要么在功能中单击时收集文本,要么将文本传递给该功能。 1-收集函数中的文本

function populateTextarea() {
    //get the text by id or predefined or however you wish or passed to function
    var txt = document.getElementById("result").value;

    document.getElementById("ID of the textarea").value = txt;
 }
<input type="button" value="xxxxx" name="jajaj" onclick="populateTextarea()">

或2-将文本传递给函数

function populateTextarea(txt) {
        //get the text by id or predefined or however you wish or passed to function


        document.getElementById("ID of the textarea").value = txt;
     }


<input type="button" value="xxxxx" name="jajaj" onclick="populateTextarea(document.getElementById('inputID').value)">

你可以用百万种方式做到这一点,但下面是我如何为有经验的人接近它:

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <style type="text/css">

  </style>
</head>
<body>
  <?php
    $value1 = 'this is value 1';
    $value2 = 'this is value 2';
  ?>
  <form>

      <div id="hiddenfieldsDiv">
        <input type="hidden" id="value1" value="<?php echo $value1; ?>" name="value1">
        <input type="hidden" id="value2" value="<?php echo $value2; ?>" name="value2">
      </div>

      <div id="visiblefieldsDiv">
        <input type="text" id="input1" value="" name="input1">
        <br/>
        <input type="text" id="input2" value="" name="input2">
      </div>

      <input type="button" value="Populate" name="button" onclick="populateTextarea()">

  </form>


  <script type="text/javascript">
    function populateTextarea() {

      var element = document.getElementById("hiddenfieldsDiv");
      var numOfChildren = element.childElementCount;

      for (var i=1; i <= numOfChildren; i++) {

        txt = document.getElementById('value'+i).value;
        document.getElementById('input'+i).value = txt;
      }

    }

  </script>

</body>
</html>