我想写一个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()">
OR 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>