Javascript函数,用于在单击按钮时填充文本框
我想编写一个javascript函数来填充文本框,从变量21、22……中获取值。。。。 值不可用的文本框(如from11、from12等)应保持空白。实际上,如果addJavascript函数,用于在单击按钮时填充文本框,javascript,php,html,Javascript,Php,Html,我想编写一个javascript函数来填充文本框,从变量21、22……中获取值。。。。 值不可用的文本框(如from11、from12等)应保持空白。实际上,如果addvalue='$from21',则框将在加载时填充。但我希望箱子在装载时保持空着。只能在单击按钮时填写 <?php for ($rn = 2; $rn <= 4; ++$rn) { for ($slot = 1; $slot <= 5; ++$slot) { ${"from" . $rn
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>";
?>
函数fun()
{
}
jQuery将更容易。您可以将值存储在隐藏输入中,然后单击“填充所有输入”
此处存储:
<input type="hidden" id="value1" value="<?php echo $value1?>" name="value1">
您应该使用对象而不是变量,并将这些值存储在我不清楚的ITU中。没有在javascript中获取“result”、“textarea的ID”、“inputID”do you know“document.getElementById”函数的引用?如果不是,我建议你在学校里通读这个函数。这是javascript最重要的功能之一。如果答案是正确的,请标记我的答案。我只是在我的答案末尾贴出了全部代码。我面临着这个问题,因为我有多个文本值。如何用一个按钮填充所有可能的框我的更新代码应该这样做。我做这件事的方式符合你的经验。请别忘了把它标对。
<input type="text" id="input1" value="" name="input1">
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()">
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>