我试图通过使用Javascript来选择名为“类型”的下拉列表的值,该值等于名为“$类型”的PHP变量的值。我知道JS中有一个“selectindex”函数,但“$ Type”不包含仅包含数字的文本。选择下拉值等于使用javascript的php变量
我试图使用我在这个网站上找到的循环,但那对我没有用。
PHP:
while(list($ArtikelID, $Type) = mysql_fetch_array($result))
{
$arid = $ArtikelID;
$te = $Type;
}
HTML + JS:
<form name="send" method="post" action="editartticlesdef.php">
articlenumber: </br>
<input readonly="readonly" name="ArticleID" value=<?php echo $arid ?>></br>
Type: </br>
<select name="Type" id="Type">
<option value="Article">Article</option>
<option value="Code">Code</option>
<option value="News">News</option>
<option value="Project">Project</option>
</select></br>
<script>
window.onload = function SelectType()
{
var sel = document.getElementById('Type');
var val = document.getElementById('<?php echo $te; ?>');
for(var i = 0, j = sel.options.length; i < j; ++i)
{
if(sel.options[i].innerHTML === val)
{
sel.selectedIndex = i;
break;
}
}
}
</script>
</form>
我没有使用Ajax或jQuery的,所以这就是为什么我试图使用循环。但是由于某种原因它不起作用。加载时,下拉菜单仍会在默认情况下选择第一个选项。
你不需要javascript来显示选定的值。你可以写条件来设置'selected'属性' – Rayon