Javascript getElementById无法获取html id的值
如果选择选择选项值3,则显示3文本框 这幅画是我想要的结果。 但现在是这样的错误结果。 使用样式显示、可见/隐藏、无 但它不起作用。我错过什么了吗 php数组转换为javascript数组, 然后,javascript中的getElementById值正在使用php中的count数组Javascript getElementById无法获取html id的值,javascript,php,jquery,html,getelementbyid,Javascript,Php,Jquery,Html,Getelementbyid,如果选择选择选项值3,则显示3文本框 这幅画是我想要的结果。 但现在是这样的错误结果。 使用样式显示、可见/隐藏、无 但它不起作用。我错过什么了吗 php数组转换为javascript数组, 然后,javascript中的getElementById值正在使用php中的count数组 <?php $arr[0] = "10.1.35.31"; $arr[1] = "10.1.35.122"; $arr[2] = "10.1.35.133"; ?> <h
<?php
$arr[0] = "10.1.35.31";
$arr[1] = "10.1.35.122";
$arr[2] = "10.1.35.133";
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/main.css">
<script type="text/javascript">
function getR() {
document.getElementById("ipcnt").value = "<?php echo count($arr); ?>";
<?php
for($i = 0; $i < count($arr); $i++) {
echo "document.getElementById(\"" . "mmeremoteip" . $i . "\").value = \"" . $arr[$i]. "\";\n";
}
?>
}
function mmeswitch(){
<?php
for($i = 0; $i < 8; $i++){
echo "document.getElementById(\"mmeremoteip". $i . "\").style.visibility = \"hidden\";\n";
echo "document.getElementById(\"mmeremoteip". $i . "\").style.display = \"none\";\n";
}
for($i = 0; $i < 8; $i++){
echo "if( document.getElementById(\"ipcnt\").value == " . ($i+1) . " ) { \n";
for($j = 0; $j < $i+1; $j++) {
echo "document.getElementById(\"mmeremoteip". $j . "\").style.visibility = \"visible\";\n";
echo "document.getElementById(\"mmeremoteip". $j . "\").style.display = style_display_on();\n";
}
echo "}\n";
}
?>
</script>
</head>
<body onLoad="getR();">
<form name="save" method="post">
<h1>TEST</h1>
<table>
<tr id="ipcnt"><td colspan="2">No. of IP</td>
<td><select name="ipcnt" size="1" onChange="mmeswitch();">
<?php
for($i = 1; $i <= 8; $i++) {
echo "<option value='". $i ."'>". $i ."</option>\n";
}
?>
</select></td>
</tr>
<?php
for($i = 0; $i < 8; $i++) {
echo "<tr id=\"mmeremoteip".$i."\"><td class=\"param\" colspan=\"2\">MME Remote IP" . ($i+1) . "</td>\n";
echo "<td><input type=\"text\" name=\"mmeremoteip" . $i . "\" class=\"inputParam\" size=\"20\" maxlength=\"15\" value=\"".$arr[$i] ."\"></td></tr>\n";
}
?>
</table>
</body>
</html>
在这篇文章中,我用vanilla js发布了一个代码的工作示例。这方面的js代码是:
document.body.onload = init;
let arr = [1,2,3,4,5]; // Array is from php like
/*
let arr = JSON.parse('<?php echo json_encode($arr); ?>'); // In case you don't know what is does read more about json
*/
// function called when page is loaded
function init () {
// this creates textfields for each 'ip'
for(let i=0;i<arr.length;i++){
let el = document.createElement('input'); // create textfield
el.setAttribute('type','text'); // set it to text field
el.className = 'textField'; // set class for later use
el.id = 'textField'+i; // set id
el.value = arr[i]; // set value(IP)
document.getElementById('container').appendChild(el); // add to the page
}
document.getElementById('textFieldSelect').addEventListener('change',onSelectChange); // add action to select
onSelectChange(); // "trigger" select when page is loaded
}
function onSelectChange(){
var val = document.getElementById('textFieldSelect').value; // how many textfields shall there be
// hide all textfields
document.querySelectorAll('.textField').forEach(function(elem,idx){
elem.style = 'display: none';
});
//show exactly 'val' textfields
for(let i = 0;i<val;i++){
document.getElementById('textField'+i).style='display:block;';
}
}
在这篇文章中,我用vanilla js发布了一个代码的工作示例。这方面的js代码是:
document.body.onload = init;
let arr = [1,2,3,4,5]; // Array is from php like
/*
let arr = JSON.parse('<?php echo json_encode($arr); ?>'); // In case you don't know what is does read more about json
*/
// function called when page is loaded
function init () {
// this creates textfields for each 'ip'
for(let i=0;i<arr.length;i++){
let el = document.createElement('input'); // create textfield
el.setAttribute('type','text'); // set it to text field
el.className = 'textField'; // set class for later use
el.id = 'textField'+i; // set id
el.value = arr[i]; // set value(IP)
document.getElementById('container').appendChild(el); // add to the page
}
document.getElementById('textFieldSelect').addEventListener('change',onSelectChange); // add action to select
onSelectChange(); // "trigger" select when page is loaded
}
function onSelectChange(){
var val = document.getElementById('textFieldSelect').value; // how many textfields shall there be
// hide all textfields
document.querySelectorAll('.textField').forEach(function(elem,idx){
elem.style = 'display: none';
});
//show exactly 'val' textfields
for(let i = 0;i<val;i++){
document.getElementById('textField'+i).style='display:block;';
}
}
您能提供呈现版本-ie而不需要所有php混淆吗?看起来您可能有额外的空间,但无法确定。您已经在js函数中插入了php代码抱歉!!我输入php数组代码。。!!将php数组映射到js数组,然后使用纯js,这绝对是一个更好的主意。我如何使用纯js。。。?那个开关功能??哪一个?您可以提供呈现版本-ie而不需要所有php模糊处理?看起来您可能有额外的空间,但无法确定。您已经在js函数中插入了php代码抱歉!!我输入php数组代码。。!!将php数组映射到js数组,然后使用纯js,这绝对是一个更好的主意。我如何使用纯js。。。?那个开关功能??哪一个??