我正在尝试将由管道分隔的值发送到2个不同的字段。由于某种原因,它将无法正常工作。

有任何想法吗?

<script type="text/javascript">
function folder(selectVeld)
{
   // Stuur informatie terug
   var id = (selectVeld.options[selectVeld.selectedIndex].value).split('|');
   // Id
   document.getElementsByName('folder_id').value = id[0];
   // Naam
   document.getElementsByName('folder_naam').value = id[1];
}
</script>

<tr>
    <td><?php echo $lang['folder']; ?>:</td>
    <td><select name="folder" onChange="folder(this)">
    <option value="geen"><?php echo $lang['no_folder']; ?></option>
    <?php
        $sql = "SELECT id, naam FROM 2_folder ORDER BY naam ASC";
        $res = mysql_query($sql,$con);
        while ($row = mysql_fetch_assoc($res)){
    ?>
        <option <?php if($row['id'].'|'.$row['naam'] == $folder){ echo 'selected="selected"'; } ?> value="<?php echo $row['id'].'|'.$row['naam']; ?>"><?php echo $row['naam'] ?></option>
    <?php } ?></select>
    </td>
</tr>

    <input type="text" name="folder_id" value="<?php echo $folder_id; ?>" size="5" style="background-color: #e7e7e9" readonly="readonly" />

    <input type="text" name="folder_naam" value="<?php echo $folder_naam; ?>" size="5" style="background-color: #e7e7e9" readonly="readonly" />

最佳答案

document.getElementsByName返回元素的array,即使只有一个具有该名称的元素也是如此。

document.getElementsByName('folder_id')[0].value = id[0];
document.getElementsByName('folder_naam')[0].value = id[1];


应该可以。 jsfiddle sample

10-07 23:27