为什么我无法使用 JQuery 检索插入到 textarea 标签中的文本?
Why I can't retrieve the text inserted into a textarea tag using JQuery?
我是 JavaScript 和 JQuery 的新手,我遇到了以下问题。
进入一个页面我有以下 textarea 有 id="notaCorrente":
<div class="form-group">
<label for="comment">Inserire una nota:</label>
<textarea id="notaCorrente" class="form-control" rows="5"></textarea>
</div>
用户可以在其中插入一些文本。
我不想将此文本区域提交到表单中(没有表单),但我想使用 JQuery 检索插入的值(因为那时我必须执行 AJAX 使用检索到的文本调用而不是 post 提交)。
所以我是这样做的:
function rimettiInLavorazioneProgetto() {
alert("INTO rimettiInLavorazioneProgetto()");
var testoNotaCorrente = $('#notaCorrente').text();
alert("NOTA CORRENTE: " + testoNotaCorrente);
}
如您所见,我尝试检索具有 id="notaCorrente" 的对象内的文本,但它不起作用。我进入该方法,但是当打印第二个警报时,如果我在我的文本区域中插入了一些文本,检索到的文本也总是空的。
为什么?我错过了什么?我该如何解决这个问题?
Tnx
使用val()
not text()
, because it will not return correct value sometimes. For more visit val() vs. text() for textarea
function rimettiInLavorazioneProgetto() {
alert("INTO rimettiInLavorazioneProgetto()");
var testoNotaCorrente = $('#notaCorrente').val();
alert("NOTA CORRENTE: " + testoNotaCorrente);
}
我是 JavaScript 和 JQuery 的新手,我遇到了以下问题。
进入一个页面我有以下 textarea 有 id="notaCorrente":
<div class="form-group">
<label for="comment">Inserire una nota:</label>
<textarea id="notaCorrente" class="form-control" rows="5"></textarea>
</div>
用户可以在其中插入一些文本。
我不想将此文本区域提交到表单中(没有表单),但我想使用 JQuery 检索插入的值(因为那时我必须执行 AJAX 使用检索到的文本调用而不是 post 提交)。
所以我是这样做的:
function rimettiInLavorazioneProgetto() {
alert("INTO rimettiInLavorazioneProgetto()");
var testoNotaCorrente = $('#notaCorrente').text();
alert("NOTA CORRENTE: " + testoNotaCorrente);
}
如您所见,我尝试检索具有 id="notaCorrente" 的对象内的文本,但它不起作用。我进入该方法,但是当打印第二个警报时,如果我在我的文本区域中插入了一些文本,检索到的文本也总是空的。
为什么?我错过了什么?我该如何解决这个问题?
Tnx
使用val()
not text()
, because it will not return correct value sometimes. For more visit val() vs. text() for textarea
function rimettiInLavorazioneProgetto() {
alert("INTO rimettiInLavorazioneProgetto()");
var testoNotaCorrente = $('#notaCorrente').val();
alert("NOTA CORRENTE: " + testoNotaCorrente);
}