我有一个简单的 js 脚本,我一直在使用它来根据从下拉列表中选择的值加载文本框。
问题:它只在更改时加载,如果加载时选择的值不是 0,我需要在加载页面时加载它。这个脚本让我很困惑,所以我无法确定我需要的确切位置更新。
剧本:
function addInput(){
var arr = [
"<p style='text-align:center;'>Reward 1: <input type='text' name='rew1' id='rew1' value='<?php echo $connt['reward_1'] ?>'></p>",
"<p style='text-align:center;'>Reward 2: <input type='text' name='rew2' id='rew2' value='<?php echo $connt['reward_2'] ?>'></p>",
"<p style='text-align:center;'>Reward 3: <input type='text' name='rew3' id='rew3' value='<?php echo $connt['reward_3'] ?>'></p>",
"<p style='text-align:center;'>Reward 4: <input type='text' name='rew4' id='rew4' value='<?php echo $connt['reward_4'] ?>'></p>",
"<p style='text-align:center;'>Reward 5: <input type='text' name='rew5' id='rew5' value='<?php echo $connt['reward_5'] ?>'></p>",
"<p style='text-align:center;'>Reward 6: <input type='text' name='rew6' id='rew6' value='<?php echo $connt['reward_6'] ?>'></p>",
"<p style='text-align:center;'>Reward 7: <input type='text' name='rew7' id='rew7' value='<?php echo $connt['reward_7'] ?>'></p>",
"<p style='text-align:center;'>Reward 8: <input type='text' name='rew8' id='rew8' value='<?php echo $connt['reward_8'] ?>'></p>",
"<p style='text-align:center;'>Reward 9: <input type='text' name='rew9' id='rew9' value='<?php echo $connt['reward_9'] ?>'></p>",
"<p style='text-align:center;'>Reward 10: <input type='text' name='rew10' id='rew10' value='<?php echo $connt['reward_10'] ?>'></p>",
];
var el = document.getElementById('select');
var div = document.getElementById('rewards');
div.style.display = "block";
while(div.hasChildNodes()) { div.removeChild(div.childNodes[0]); }
if(el.selectedIndex > 0) {
for(var i = 0; i<el.options[el.selectedIndex].value; i++) {
div.innerHTML = div.innerHTML + arr[i];
}
}
}