0

DBの値を使用して選択ボックスを作成していますが、選択したアイテムの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

あなたは次のようなことをすることができます:

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 に答える
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 に答える