Uncaught TypeError: 无法设置 null 对象的属性 (设置 ‘innerHTML’)

huangapple go评论114阅读模式
英文:

webpage, jquery passing value lose : Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')

问题

I'm trying to save the edited data while click "send message" button

here is the error message:

未捕获的类型错误:无法设置 null 的属性(设置 'innerHTML')

似乎在说 "itemValue" 为 null,但我不确定问题出在哪里?

<script>
    var storedItem = localStorage.getItem("storedItem");
    
    function save() {
      var itemValue = document.getElementById("input_edit_data").value;
      localStorage.setItem("storedItem", itemValue);
      document.getElementById("savedText").innerHTML = itemValue;
      console.log("storedItem", storedItem);
    }
</script>

我是新来的,认为大家都是优秀的专家,也许我能得到一些很酷的反馈,谢谢。

英文:

I'm trying to save the edited data while click "send message" button

here is the error message:

> Uncaught TypeError: Cannot set properties of null (setting
> 'innerHTML')

seems saying "itemValue" is none, but I'm not sure where get problem?

  &lt;script&gt;
    var storedItem = localStorage.getItem(&quot;storedItem&quot;);
    
    function save() {
      var itemValue = document.getElementById(&quot;input_edit_data&quot;).value;
      localStorage.setItem(&quot;storedItem&quot;, itemValue);
      document.getElementById(&quot;savedText&quot;).innerHTML = itemValue;
      console.log(&quot;storedItem&quot;, storedItem);
    }
  &lt;/script&gt;



  &lt;div class=&quot;modal fade&quot; id=&quot;exampleModal&quot; tabindex=&quot;-1&quot; role=&quot;dialog&quot; aria-labelledby=&quot;exampleModalLabel&quot; aria-hidden=&quot;true&quot;&gt;
    &lt;div class=&quot;modal-dialog&quot; role=&quot;document&quot;&gt;
      &lt;div class=&quot;modal-content&quot;&gt;
        &lt;div class=&quot;modal-header&quot;&gt;
          &lt;h5 class=&quot;modal-title&quot; id=&quot;exampleModalLabel&quot;&gt;device_id:&lt;/h5&gt;
          &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;
            &lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;
          &lt;/button&gt;
        &lt;/div&gt;
        &lt;div class=&quot;modal-body&quot;&gt;
          &lt;form&gt;
            &lt;div class=&quot;form-group&quot;&gt;
              &lt;label for=&quot;recipient-name&quot; class=&quot;col-form-label&quot;&gt;name:&lt;/label&gt;
              &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;recipient-name&quot;&gt;
            &lt;/div&gt;
            
            &lt;div class=&quot;form-group&quot;&gt;
              &lt;label for=&quot;field1&quot; class=&quot;col-form-label&quot;&gt;group:&lt;/label&gt;
              &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;input_edit_data&quot;&gt;
            &lt;/div&gt;
            &lt;div class=&quot;form-group&quot;&gt;
              &lt;label for=&quot;field2&quot; class=&quot;col-form-label&quot;&gt;id :&lt;/label&gt;
              &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;input_edit_data&quot;&gt;
            &lt;/div&gt;

       


            &lt;!-- End of additional form fields --&gt;
            
          &lt;/form&gt;
        &lt;/div&gt;
        &lt;div class=&quot;modal-footer&quot;&gt;
          &lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; data-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
          &lt;button type=&quot;button&quot; class=&quot;btn btn-primary&quot; onclick=&quot;save()&quot;&gt;Send message&lt;/button&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;

I'm new to this community, thinking of everyone is excellent experts, maybe I can get a cool feedback, thanks


  • the full version of code, in case needed
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;!-- Meta tags and other head elements --&gt;
&lt;title&gt;Static Template&lt;/title&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.datatables.net/1.13.4/css/jquery.dataTables.css&quot;/&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css&quot;&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css&quot; integrity=&quot;sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/html&gt;
&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;
&lt;table id=&quot;example&quot; class=&quot;display&quot;&gt;&lt;/table&gt;
&lt;!-- JavaScript libraries --&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.4.1.min.js&quot; integrity=&quot;sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.datatables.net/1.13.4/js/jquery.dataTables.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
var storedItem = localStorage.getItem(&quot;storedItem&quot;);
function save() {
var itemValue = document.getElementById(&quot;input_edit_data&quot;).value;
localStorage.setItem(&quot;storedItem&quot;, itemValue);
document.getElementById(&quot;savedText&quot;).innerHTML = itemValue;
console.log(&quot;storedItem&quot;, storedItem);
}
&lt;/script&gt;
&lt;script&gt;
$(document).ready(function () {
const getToken = localStorage.getItem(&quot;token&quot;)
console.log(&quot;GET TOKEN&quot;, getToken)
var table = $(&quot;#example&quot;).DataTable({
ajax: {
url: &quot;http://192.168.01.10:8000/api/item_status&quot;,
headers: {
&#39;Authorization&#39;: &#39;Bearer &#39; + getToken
},
dataSrc: function (response) {
console.log(&quot;DATA&quot;, response.ipcs);
return response.ipcs
}
},
columns: [
{
data: null,
title: &quot;ACTION&quot;,
render: function (data, type, row) {
return &#39;&lt;button class=&quot;btn btn-primary btn-sm edit-btn&quot; data-id=&quot;&#39; + row.device_id + &#39;&quot;&gt;Edit&lt;/button&gt;&#39; +
&#39;&lt;button class=&quot;btn btn-danger btn-sm delete-btn&quot; data-id=&quot;&#39; + row.device_id + &#39;&quot;&gt;Delete&lt;/button&gt;&#39;;
}
},
{
data: null,
title: &quot;edit&quot;,
render: function (data, type, row) {
return&#39;&lt;button class=&quot;btn btn-success btn-sm&quot; data-id=&quot;1&quot; data-toggle=&quot;modal&quot; data-target=&quot;#exampleModal&quot;&gt;Edit&lt;/button&gt;   &#39;;
}
},
{
data: null,
title: &quot;edit_new&quot;,
render: function (data, type, row) {
return&#39;&lt;button type=&quot;button&quot; class=&quot;btn btn-primary&quot; data-toggle=&quot;modal&quot; data-target=&quot;#exampleModal&quot; data-whatever=&quot; &quot;&gt;edit_new&lt;/button&gt;&#39;;
}
},
{ data: &quot;device_id&quot;, title: &quot;device_id&quot; },
{ data: &quot;device_id&quot;, title: &quot;device_id&quot; },
{ data: &quot;name&quot;, title: &quot;name&quot; },
{ data: &quot;group&quot;, title: &quot;group&quot; },
{ data: &quot;ip&quot;, title: &quot;ip&quot; },
]
});
table.draw()
var rowData; 
$(&#39;#example tbody&#39;).on(&#39;click&#39;, &#39;button&#39;, function (event) {  
var data = table.row($(this).parents(&#39;tr&#39;)).data();
rowData = data;
console.log(&quot;GET edit DATA&quot;, data)        
});
$(function(){
$(&#39;#exampleModal&#39;).on(&#39;show.bs.modal&#39;, function (event) {
var button = $(event.relatedTarget);
var recipient = button.data(&#39;whatever&#39;);
var data = rowData;
var modal = $(this);
modal.find(&#39;.modal-title&#39;).text(&#39;Update data &#39; + recipient);
var inputFields = modal.find(&#39;.modal-body input&#39;);
var keys = Object.keys(data);
for (var i = 0; i &lt; inputFields.length; i++) {
var inputValue = data[keys[i]];
$(inputFields[i]).val(inputValue);
}
});
});
function editRow_website(id){
var device_id=document.getElementById(&quot;device_id&quot;+id);  
var name=document.getElementById(&quot;name&quot;+id);  
var group=document.getElementById(&quot;group&quot;+id);
var ip=document.getElementById(&quot;ip&quot;+id);
let getEditData = {
&quot;device_id&quot;: device_id,
&quot;email_data&quot;: name,
&quot;address_data&quot;: group,
&quot;name_data&quot;: ip,
}
document.getElementById(&quot;#editid input&quot;).value = device_id
}
$(document).on(&#39;click&#39;, &#39;.delete-btn&#39;, function (event) {  
var id = $(this).data(&#39;id&#39;)
console.log(&quot;GET DLETE ID&quot;, id)
confirmDelete(id) 
});
function deleteData(id) {
$.ajax({
url: &quot;http://192.168.01.10:8000/api/item_status=&quot;+ id,
type:&#39;DELETE&#39;,
headers: {
&#39;Authorization&#39;: &#39;Bearer &#39; + getToken  
},
success: function(response){
console.log(response);
}
}).then(res =&gt; console.log(&quot;RES&quot;, res));  
}
function editRow(id) {
console.log(&quot;EDIT&quot;, getFinalData);
}
function confirmDelete(id) {
if (confirm(&quot;sure to delete this row?&quot;)== true) {
var table = $(&quot;#example&quot;).DataTable();
var row = table.row($(this).closest(&#39;tr&#39;));
deleteData(id);
if (row.length &gt; 0) {
row.remove().draw();
console.log(&quot;Delete row with id:&quot;, id);
}
} else{
none
}
}
});
&lt;/script&gt;
&lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;div class =&quot;table-title&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-sm-4&quot;&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-info add-new&quot;&gt;&lt;i class=&quot;fa fa-plus&quot;&gt;&lt;/i&gt;Add New&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;modal fade&quot; id=&quot;exampleModal&quot; tabindex=&quot;-1&quot; role=&quot;dialog&quot; aria-labelledby=&quot;exampleModalLabel&quot; aria-hidden=&quot;true&quot;&gt;
&lt;div class=&quot;modal-dialog&quot; role=&quot;document&quot;&gt;
&lt;div class=&quot;modal-content&quot;&gt;
&lt;div class=&quot;modal-header&quot;&gt;
&lt;h5 class=&quot;modal-title&quot; id=&quot;exampleModalLabel&quot;&gt;device_id:&lt;/h5&gt;
&lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;
&lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;modal-body&quot;&gt;
&lt;form&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;recipient-name&quot; class=&quot;col-form-label&quot;&gt;name:&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;recipient-name&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;field1&quot; class=&quot;col-form-label&quot;&gt;group:&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;input_edit_data&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;field2&quot; class=&quot;col-form-label&quot;&gt;id :&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;input_edit_data&quot;&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div class=&quot;modal-footer&quot;&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; data-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-primary&quot; onclick=&quot;save()&quot;&gt;Send message&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
$(function() {
$(&#39;#exampleModal&#39;).on(&#39;show.bs.modal&#39;, function(e) {
$(&#39;.modalTextInput&#39;).val(&#39;&#39;);
let btn = $(e.relatedTarget); // e.related here is the element that opened the modal, specifically the row button
let id = btn.data(&#39;id&#39;); // this is how you get the of any `data` attribute of an element
$(&#39;.saveEdit&#39;).data(&#39;id&#39;, id); // then pass it to the button inside the modal
})
$(&#39;.saveEdit&#39;).on(&#39;click&#39;, function() {
let id = $(this).data(&#39;id&#39;); // the rest is just the same
saveNote(id);
$(&#39;#exampleModal&#39;).modal(&#39;toggle&#39;); // this is to close the modal after clicking the modal button
})
})
function saveNote(id) {
let text = $(&#39;.modalTextInput&#39;).val();
$(&#39;.recentNote&#39;).data(&#39;note&#39;, text);
console.log($(&#39;.recentNote&#39;).data(&#39;note&#39;));
console.log(text + &#39; --&gt; &#39; + id);
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: -2

document.getElementById("savedText") 返回 null,因此它没有 innerHTML 可以引用。

你可以将

document.getElementById("savedText").innerHTML = itemValue;

改为

let savedText = document.getElementById("savedText");
if (savedText) {
    savedText.innerHTML = itemValue;
}

这样会检查元素是否被找到,并且只有在元素存在时才设置它的 innerHTML,以防止错误发生。

但是你的元素之所以缺失是有原因的,你需要找出为什么。可能是你根本就没有这样的元素,根据你分享的代码,似乎是这种情况,其中没有这样的标签,或者在 Javascript 评估时未加载该元素。

英文:

document.getElementById(&quot;savedText&quot;) returns null, so it does not have an innerHTML to refer to.

You can change

document.getElementById(&quot;savedText&quot;).innerHTML = itemValue;

to

let savedText = document.getElementById(&quot;savedText&quot;);
if (savedText) {
savedText.innerHTML = itemValue;
}

That would check whether the element was found and only set its innerHTML if it exists to prevent the error from occurring.

But your element is missing for a reason and you will need to find out why. You either do not have such an element at all, which seems to be the case based on the code you have shared, where no such tag exists, or it was not loaded when your Javascript evaluates.

huangapple
  • 本文由 发表于 2023年6月19日 16:04:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定