无法使用getElementById在var上插入HTML [英] Cannot Insert HTML on var using getElementById
问题描述
我正在尝试使用控制器向div插入HTML,但是我不知道JS代码有什么问题,它总是返回Uncaught TypeError:无法将属性'innerHTML'设置为null.我有什么想念的吗?
I am trying to insert an HTML using my controller to a div, but I do not know what is wrong with my JS code, it always return Uncaught TypeError: Cannot set property 'innerHTML' of null. Is there anything I missed?
我的控制器:
$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>";
}
echo $ selectoption;
echo $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选择元素时,请使用#
.
remove the hash #
.use #
when you use jquery to select element by id.
document.getElementById('htmlcontainer')
如果您使用的是jquery,则应使用#(表示id)
if you are using jquery then you should use # ( for id)
$('#htmlcontainer').html('text');
修改
另一个问题
您已直接在div标签中添加了div标签.select标签不能直接具有div标签,仅允许<option>
或<optgroup>
元素,因此必须对其进行更改.
you have added div tags inside select tag directly.select tag cannot have div tags directly only <option>
or <optgroup>
elements are allowed.so you have to change it.
这篇关于无法使用getElementById在var上插入HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!