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

标签 javascript php html

我想编写一个 JavaScript 函数,以便填充文本框,并从变量 from21、from22、... 中获取值。 值不可用的文本框(例如 from11、from12...等)应保留为空白。实际上,如果添加 value='$from21',那么这些框将在加载时填充。但我希望箱子在装载时保持空状态。这些应该仅在单击按钮时填写。

<?php
for ($rn = 2; $rn <= 4; ++$rn) {
    for ($slot = 1; $slot <= 5; ++$slot) {
        ${"from" . $rn . $slot} = $slot + $rn;
        ${"no" . $rn . $slot}   = $slot - $rn;
    }
}
?>

    <html>
    <script type="text/javascript">

    function fun()
    {


    }

    </script>
    </html>
    <?php
echo "<body>
     <input type='text' id='from11'/> <input type='text' id='no11'/>  <br/>
     <input type='text' id='from12'/> <input type='text' id='no12'/>  <br/>
     <input type='text' id='from13'/> <input type='text' id='no13'/>  <br/>
     <input type='text' id='from14'/> <input type='text' id='no14'/>  <br/>
     <input type='text' id='from15'/> <input type='text' id='no15'/>  <br/>
     <input type='text' id='from16'/> <input type='text' id='no16'/>  <br/>

     <input type='text' id='from31'/> <input type='text' id='no31'/>  <br/>
     <input type='text' id='from32'/> <input type='text' id='no32'/>  <br/>
     <input type='text' id='from33'/> <input type='text' id='no33'/>  <br/>
     <input type='text' id='from34'/> <input type='text' id='no34'/>  <br/>
     <input type='text' id='from35'/> <input type='text' id='no35'/>  <br/>
     <input type='text' id='from36'/> <input type='text' id='no36'/>  <br/>
      ";
?>
     <html>
     <input type="button" value="Fill" id="fun1" onclick="fun( )"/>

       </html>
       <?php
echo "</body>";
?>

最佳答案

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>

关于Javascript 函数在单击按钮时填充文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34740043/

相关文章:

html - 如何检查符合 html5/css3 的网页

javascript - 为什么这个递归函数会覆盖第二次调用的值?

javascript - 在文本区域内查找正文元素

php - Laravel 上传文件到项目目录外的不同存储

jquery - 使用 Jquery 检测 anchor ?

html - margin 权利不接受较小的屏幕

javascript - Google map API 示例不起作用

javascript - Node.js async.each - "callback was already called"

php - 根据之前的表单输入和来自 MySQL 的数据填充下拉列表

PHPUnit 进度点在新行中并显示 "wrong"百分比