可能是一个非常简单的问题,但我似乎找不到将一段数据从 textarea 发送到 javascript/typescript 函数的方法。
HTML
<div data-options="dxContent : { targetPlaceholder: 'content' } " >
<p> use this page to log errors</p>
<p id="prevErrors" style="font-style:italic">PreviousErrors</p>
<br />
<div id="curError" data-bind="dxTextArea: { }"></div>
<br />
<div data-bind="dxButton: { text: 'Save and return to main menu' }" onclick=" saveMyData(document.getElementById(curError).innerText)"></div>
</div>
Java/TypeScript
<script>
function saveMyData(curError) {
//this will be where data is saved or emailed
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
if (dd < 10) {
dd = '0' + dd
}
if (h < 10) {
h = "0" + h;
}
if (s < 10)
{
s = "0"+s;
}
if (m < 10) {
m = "0"+m;
}
if (mm < 10) {
mm = '0' + mm
}
today = dd + '/' + mm + '/' + yyyy +" , "+h+":"+m+":"+s;
prevErrors.innerText += "\n "+today+", "+curError;
}
</script>
直到最近才发现 typescript 和 javascript 几乎是相同的,我感到有点尴尬,启动 typescript 是一场这样的战斗 - 与 c#/vb/etc 完全不同。
问题
我一直在尝试将数据从这个dxtextArea
控件发送到一个函数。如您所见,我已经尝试过使用innerText,但是,这似乎没有向函数发送任何内容(出现错误:
'Uncaught TypeError: Cannot read property 'innerText' of null', line 1, file 'http://localhost:51828/index.html#'.
即使 textArea 中存在数据。
任何人都遇到过这个问题/知道如何解决它/或者这只是我对 Java/typescript 知识的愚蠢和天真?
编辑
我被告知将我的方法调用从 innerText 更改为 value(并在 ID 周围添加引号),但这仍然没有解决问题:
saveMyData(document.getElementById('curError').value)"
编辑 2我的完整脚本:
<div data-options="dxView : { name: 'ErrorLogging', title: 'ErrorLogging' } " >
<div data-options="dxContent : { targetPlaceholder: 'content' } " >
<p>Use this page to log errors</p>
<br />
<p style="font-style:italic">PreviousErrors</p>
<p id="prevErrors" style="font-style:italic"></p>
<br />
<div id="curError" data-bind="dxTextArea: { }" ></div>
<br />
<div data-bind="dxButton: { text: 'Save and return to main menu' }" onclick="saveMyData(document.getElementById('curError').textContent)"></div>
</div>
</div>
<script>
function saveMyData(elem) {
//this will be where data is saved or emailed
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
//add a '0' in front of single digit values
if (dd < 10) {
dd = '0' + dd
}
if (h < 10) {
h = "0" + h;
}
if (s < 10)
{
s = "0"+s;
}
if (m < 10) {
m = "0"+m;
}
if (mm < 10) {
mm = '0' + mm
}
if (elem.textContent) {
var curError = elem.textContent ?
elem.textContent :
elem.innerText;
}
alert(curError);
today = dd + '/' + mm + '/' + yyyy +" , "+h+":"+m+":"+s; //created string containing todays' date and time
prevErrors.innerText += "\n " + today + ", " + elem;
}
</script>