如何使用 javascript 从 TextArea 获取值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8065487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:07:59  来源:igfitidea点击:

How to get a value from TextArea using javascript?

javascriptjqueryhtml

提问by neurotix

Well this question might look simillar to questions easily findable with google but for some reason all solutions out there have not worked for me. All I want to do is to check if there is text inside of textarea.

好吧,这个问题可能看起来与谷歌很容易找到的问题相似,但由于某种原因,所有的解决方案都对我不起作用。我想要做的就是检查 textarea 中是否有文本。

JS snippet:

JS片段:

var PrzedDzier = document.getElementById('ctl00_m_g_87871370_ce46_4f47_b1eb_614d0106535d_ff105_1_ctl00_ctl00_TextField');   
var a = PrzedDzier.val();
if(a == "")
{
  alert('FU');
  result = false;
}

HTML baby:

HTML宝贝:

<textarea dir="none" class="ms-long" title="PDzier" id="ctl00_m_g_87871370_ce46_4f47_b1eb_614d0106535d_ff105_1_ctl00_ctl00_TextField" cols="20" rows="2" name="ctl00$m$g_87871370_ce46_4f47_b1eb_614d0106535d$ff105_1$ctl00$ctl00$TextField"></textarea>

Debugger (Firebug) stops working at var a = PrzedDzier.val();so I assume it's something wrong with val()method...

调试器(Firebug)停止工作,var a = PrzedDzier.val();所以我认为val()方法有问题......

回答by James Hill

You have retrieved the object as a JS object, not a jQuery object. You need to use the jQuery ID selector to retrieve the jQuery object:

您已将对象检索为 JS 对象,而不是 jQuery 对象。您需要使用 jQuery ID 选择器来检索 jQuery 对象:

var PrzedDzier = $("#ctl00_m_g_87871370_ce46_4f47_b1eb_614d0106535d_ff105_1_ctl00_ctl00_TextField");
var a = PrzedDzier.val();
if(a == "")
{
  alert('FU');
  result = false;
}

If you would like to access the text using pure JS, simply use the valueproperty:

如果您想使用纯 JS 访问文本,只需使用以下value属性:

PrzedDzier = document.getElementById('ctl00_m_g_87871370_ce46_4f47_b1eb_614d0106535d_ff105_1_ctl00_ctl00_TextField');   
var a = PrzedDzier.value;
if(a == "")
{
  alert('FU');
  result = false;
}

回答by clklachu

Since you are using jQuery you can directly access the textarea and get the value as below.

由于您使用的是 jQuery,您可以直接访问 textarea 并获取如下值。

$("#ctl00_m_g_87871370_ce46_4f47_b1eb_614d0106535d_ff105_1_ctl00_ctl00_TextField").val();

But usually, it is more error prone to get an element using its runtime ID (if there are easy chances that the element would be moved which would change the runtime ID), otherwise you can very well use the above code.

但通常情况下,使用其运行时 ID 获取元素更容易出错(如果元素很容易被移动从而改变运行时 ID),否则您可以很好地使用上述代码。

回答by niko

I believe using strlen is better than comparing with "" because strlen is much faster and "" takes 6 bytes of ram.just letting you know the other way

我相信使用 strlen 比与 "" 比较要好,因为 strlen 快得多,而 "" 需要 6 个字节的内存。只是让你知道另一种方式

var j = document.getElementById('idoftextbox').value;
 if (strlen(j) == 0) 
  {
    alert('FU');
    return false;
  }