0

我用我的数据库中的值制作了一个选择框,我想将所选项目的 ID 写入文本框。

怎么做到呢?我有这个代码..

<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>

和..

<script>
function run() {
    document.getElementById("valgtID").value = document.getElementById("valgtTitel").value;
}
</script>

我明白了

INSERT INTO `mah1233411190550`.`konkurrence` ( `konID` , `virkID` , `konTitel` , `konBeskriv` , `konMaal` , `konMaaltype` , `konStart` , `konSlut`, `kontypeID`, `holdID` ) VALUES (NULL , '1', '1', 'cykle', '500', 'km', '2013-01-01', '2018-05-03', '1', '' );

所以 konTitel 和 kontypeID 是一样的,这是为什么呢?

4

2 回答 2

0

所以是这样的??

                <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>
于 2012-12-19T00:06:00.313 回答
0

您可以执行以下操作:

var valgtTitel = document.getElementById("valgtTitel");
document.getElementById("valgtID").value = valgtTitel.options[valgtTitel.selectedIndex].value;

这是小提琴:http: //jsfiddle.net/viralpatel/JYHLM/

编辑: 也许您可能希望将标题(除了 ID)保存在其他文本框中并从请求中检索它:

var valgtTitel = document.getElementById("valgtTitel");
document.getElementById("valgtTitle").value = valgtTitel.options[valgtTitel.selectedIndex].innerHTML;

请注意我们如何使用innerHTML而不是value获取标题。

于 2012-12-18T23:19:16.987 回答