Javascript 将select的内容拆分为不同的字段
我试图将由管道分隔的值发送到两个不同的字段。由于某种原因,它不会起作用 有什么想法吗Javascript 将select的内容拆分为不同的字段,javascript,events,Javascript,Events,我试图将由管道分隔的值发送到两个不同的字段。由于某种原因,它不会起作用 有什么想法吗 <script type="text/javascript"> function folder(selectVeld) { // Stuur informatie terug var id = (selectVeld.options[selectVeld.selectedIndex].value).split('|'); // Id document.getElementsB
<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" />
功能文件夹(选择级别)
{
//斯图尔信息中心
变量id=(selectVeld.options[selectVeld.selectedIndex].value).split(“|”);
//身份证
document.getElementsByName('folder_id')。value=id[0];
//纳姆
document.getElementsByName('folder_naam')。value=id[1];
}
:
返回元素的数组
,即使只有一个元素具有该名称
document.getElementsByName('folder_id')[0].value = id[0];
document.getElementsByName('folder_naam')[0].value = id[1];
我们应该做到这一点 我避免了这个问题,只使用文件夹id,在发送表单后通过单独的查询查找名称。什么是nr
?尝试将nr
替换为0
。哦,对不起。[nr]在试图找到解决方案时被留在了那里。我已经更新了代码。它返回一个节点列表
,而不是实际的数组。即使将[]或[0]添加到表单字段中,也没有结果。document.getElementById也不起作用。@muiter什么不起作用?选择列表是否为空?你能发布生成的HTML代码吗?选择列表正在运行,我可以在发送表单后使用$folder。问题是$row['id'].|'.$row['naam']的值没有发送到表单字段。