gpt4 book ai didi

javascript - 无法使用 getElementById 在 var 上插入 HTML

转载 作者:行者123 更新时间:2023-12-03 06:32:36 26 4
gpt4 key购买 nike

我试图使用我的 Controller 将 HTML 插入到 div 中,但我不知道我的 JS 代码出了什么问题,它总是返回 Uncaught TypeError: Cannot set property 'innerHTML' of null。有什么我错过的吗?

我的 Controller :

$this->load->model('model_admin','modeladmin');

$branchid = $this->input->post('subject_branchid');

$classdata = $this->modeladmin->getclassesviabranch('169APC00002');

$selectoption = "";

foreach ($classdata as $classitems)
{

$selectoption .= "<option value='".$classitems['class_id']."'>".$classitems['class_name']."</option>";
}

回显$selectoption;

我的看法

<select class="form-control" id="subject_branchid" name="subject_branchid" style="width:50%;" onchange="getvalues()">

<?php

foreach ($branches as $branch)
{
echo "<option value='".$branch['branch_id']."'>".$branch['branch_prefix']."</option>";
}

?>

</select>

<br>
<input type="text" id="price" name="price" />
<br>


<select class="form-control" id="subject_classid" name="subject_classid" style="width:50%;">

<div id="htmlcontainer" name="htmlcontainer"></div>

</select>

我的JS

<script>
function getvalues()
{
//get the value of the select when it changes
var value = $("#subject_branchid").val()

//make an ajax request posting it to your controller
$.post('<?=site_url("admin/test")?>', {data:value},function(result)
{
//change the input price with the returned value
//$('#price').val(result);
document.getElementById('#htmlcontainer').innerHTML = '<h1>Something</h1>';
});
};

最佳答案

删除哈希值# .使用#当你使用jquery通过id选择元素时。

 document.getElementById('htmlcontainer')

如果你使用jquery,那么你应该使用#(代表id)

$('#htmlcontainer').html('text');

编辑

另一个问题

您直接在select标签内添加了div标签。select标签不能直接有div标签<option><optgroup>元素是允许的。所以你必须更改它。

read more

关于javascript - 无法使用 getElementById 在 var 上插入 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38381181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com