Database 从选择框到文本字段的getElementById
我用数据库中的值制作了一个选择框,我想将所选项目的ID写入文本框 怎样才能做到呢?我有这个密码Database 从选择框到文本字段的getElementById,database,getelementbyid,drop-down-menu,Database,Getelementbyid,Drop Down Menu,我用数据库中的值制作了一个选择框,我想将所选项目的ID写入文本框 怎样才能做到呢?我有这个密码 <form method="POST" action="" id="submitKon"> <fieldset> <table class="opretTable" border="0"> <tr> <td width="180" height="30"><label for="valgtkon" cla
<form method="POST" action="" id="submitKon">
<fieldset>
<table class="opretTable" border="0">
<tr>
<td width="180" height="30"><label for="valgtkon" class="labelLeft">Vælg konkurrencetype</label></td>
<td><select id="valgtTitel" name="valgtTitel" onchange="run()">
<?php
$virksomhedsID = $_SESSION['virkID'];
$sql = "SELECT * FROM konkurrenceType ORDER BY konkurrenceType.kontypeID";
$result = mysql_query($sql) or die(mysql_error());
echo '<option value="Vælg type">Vælg type</option>';
while($rowSelect = mysql_fetch_assoc($result))
{
echo '<option value="' . $rowSelect['kontypeID'] . '">' . $rowSelect['kontypeTitel'] . '</option>';
}?>
</select>
那么konTitel和kontypeID是一样的,这是为什么?您可以执行以下操作:
var valgtTitel = document.getElementById("valgtTitel");
document.getElementById("valgtID").value = valgtTitel.options[valgtTitel.selectedIndex].value;
这是小提琴:
编辑:
也许您希望将标题(除ID外)保存在其他文本框中,并从请求中检索它:
var valgtTitel = document.getElementById("valgtTitel");
document.getElementById("valgtTitle").value = valgtTitel.options[valgtTitel.selectedIndex].innerHTML;
请注意,我们是如何使用
innerHTML
而不是value
来获取标题的。这样的话呢
<tr>
<td width="180" height="30"><label for="valgtkon" class="labelLeft">Vælg konkurrencetype</label></td>
<td><select id="select" name="select" onchange="run()">
<?php
$virksomhedsID = $_SESSION['virkID'];
$sql = "SELECT * FROM konkurrenceType ORDER BY konkurrenceType.kontypeID";
$result = mysql_query($sql) or die(mysql_error());
echo '<option value="Vælg type">Vælg type</option>';
while($rowSelect = mysql_fetch_assoc($result))
{
$kontypeID = $rowSelect['kontypeID'];
$kontypeTitel = $rowSelect['kontypeTitel'];
echo '<option value="' . $kontypeID . '">' . $kontypeTitel . '</option>';
}?>
</select></td>
<script>
function run() {
var select = document.getElementById("select");
document.getElementById("valgtID").value = valgtTitel.options[select.selectedIndex].innerHTML; //how to make this right?
document.getElementById("valgtTitel").value = valgtTitel.options[select.selectedIndex].innerHTML; //how to make this right?
}
</script>
<td><input type="text" name="valgtID" id="valgtID"/><input type="text" name="valgtTitel" id="valgtTitel"/></td>
</tr>
Vælg konkurrencetype
函数运行(){
var select=document.getElementById(“select”);
document.getElementById(“valgtID”).value=valgtitel.options[select.selectedIndex].innerHTML;//如何正确设置?
document.getElementById(“valgtitel”).value=valgtitel.options[select.selectedIndex].innerHTML;//如何正确设置?
}
您好,谢谢您的回答。它是一样的,konTitel和kontypeID是一样的,为什么呢?插入mah1233411190550
konkurrence
(konID
,virkID
,konitel
,konBeskriv
,konMaal
,konMaaltype
,konstrut
,kontypeID
,holdID
)值(NULL,'1','3','løfte','200','kg','2013-01-01','2018-05-03','3',';你的问题是你想用所选项目更新文本框的值。这就是你要找的吗?看来你是在以后保存这些值并在那里得到一些差异。是的,你是对的。它实际上写了id,所以我想问题解决了。我试着从selectbox和textfield以及inse中获取id和标题使用$kontypeID=$\u POST['valgtID'];$konTitel=$\u POST['valgtitel']再次进行rt;但我的sql要么写两次valgtID,要么写两次Valgtitel,你知道这是为什么吗?一旦selectbox被提交,你就会得到它的值。在你的例子中,selectbox和textbox的值都是ID,你就得到ID。你应该在页面呈现时将值保存在页面的某个地方。@ViralPatel你觉得怎么样?@X.Jacobs你做了什么啊!你知道我的密码有什么问题吗?
var valgtTitel = document.getElementById("valgtTitel");
document.getElementById("valgtTitle").value = valgtTitel.options[valgtTitel.selectedIndex].innerHTML;
<tr>
<td width="180" height="30"><label for="valgtkon" class="labelLeft">Vælg konkurrencetype</label></td>
<td><select id="select" name="select" onchange="run()">
<?php
$virksomhedsID = $_SESSION['virkID'];
$sql = "SELECT * FROM konkurrenceType ORDER BY konkurrenceType.kontypeID";
$result = mysql_query($sql) or die(mysql_error());
echo '<option value="Vælg type">Vælg type</option>';
while($rowSelect = mysql_fetch_assoc($result))
{
$kontypeID = $rowSelect['kontypeID'];
$kontypeTitel = $rowSelect['kontypeTitel'];
echo '<option value="' . $kontypeID . '">' . $kontypeTitel . '</option>';
}?>
</select></td>
<script>
function run() {
var select = document.getElementById("select");
document.getElementById("valgtID").value = valgtTitel.options[select.selectedIndex].innerHTML; //how to make this right?
document.getElementById("valgtTitel").value = valgtTitel.options[select.selectedIndex].innerHTML; //how to make this right?
}
</script>
<td><input type="text" name="valgtID" id="valgtID"/><input type="text" name="valgtTitel" id="valgtTitel"/></td>
</tr>