javascript 如果文本框为空,如何在页面加载时获取文本框的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17359236/
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
How to get value of textbox on page load, if it is null?
提问by Rishi Jagati
- Want to check first that, Is text box contains nothing on page load ?
Then assign some value to it.
if (document.getElementById("txtBoxID").value == null) { document.getElementById("txtBoxID").value = "Some text here"; }
But getting error "JavaScript runtime error: Unable to set property 'value' of undefined or null reference" How to do this?
- 想先检查一下,页面加载时文本框是否不包含任何内容?
然后为其分配一些值。
if (document.getElementById("txtBoxID").value == null) { document.getElementById("txtBoxID").value = "Some text here"; }
但是收到错误“JavaScript 运行时错误:无法设置未定义或空引用的属性‘值’”怎么办?
回答by mattn
Are you doing your code above after the page is loaded?
页面加载后你是否在执行上面的代码?
window.onload = function() {
if (document.getElementById("txtBoxID").value == null) {
document.getElementById("txtBoxID").value = "Some text here";
}
}
Note that window.onload
isn't best way to check whether page is loaded. It depends on the browser's spec. See window.addEventListener or window.attachEvent or IE.
请注意,这window.onload
不是检查页面是否已加载的最佳方法。这取决于浏览器的规范。请参阅 window.addEventListener 或 window.attachEvent 或 IE。
回答by Zaheer Ahmed
It is because your DOM is not ready so wait until load complete:
这是因为你的 DOM 还没有准备好所以等待加载完成:
window.onload=function(){
if (document.getElementById("txtBoxID").value == null) {
document.getElementById("txtBoxID").value = "Some text here";
}
};
回答by Fabrizio Calderan
You're probably trying to access the element before you defined in page, so you should execute that code on window.load
event or move that code just before </body>
您可能在页面中定义之前尝试访问该元素,因此您应该在window.load
事件上执行该代码或在此之前移动该代码</body>
as alternative you may use the placeholder
attribute supported on many modern browsers
作为替代,您可以使用placeholder
许多现代浏览器支持的属性
<input type="text" id="txtBoxId" placeholder="Some text here" />
If the input has no value, the placeholder value will be shown instead
(polyfills of this behaviour are available for older browser)
如果输入没有值,则将显示占位符值
(此行为的 polyfills 可用于旧浏览器)
回答by Jaay
You are trying to access the property 'value' on a null (or undefined) object, wich cause an exception, assuming your ID may not be txtBoxID
您正在尝试访问空(或未定义)对象上的属性“值”,这会导致异常,假设您的 ID 可能不是 txtBoxID
回答by mortb
It is not safe to test null for the textbox value. You should use empty string instead. I created a jsFiddle which shows this:
为文本框值测试 null 是不安全的。您应该改用空字符串。我创建了一个 jsFiddle,它显示了这一点:
<input type="text" id="txtBx" />
document.getElementById("txtBx").value = null;
alert("test 1 textbox is null \n" + (document.getElementById("txtBx").value == null));
alert("test 2 textbox is empty string \n" + (document.getElementById("txtBx").value == ""));
(I'm using Chrome)
(我正在使用 Chrome)
回答by Mike Lambropoulos
If you have JQuery available here is another way of accomplishing what you are asking for. In the document.ready function you can get the value of your text using the id of your textbox and wrapping it in JQuery. This will allow you to check and replace the value of your text box in the case that it is empty by using the .val() function
如果您有可用的 JQuery,这里是完成您所要求的另一种方式。在 document.ready 函数中,您可以使用文本框的 id 获取文本的值并将其包装在 JQuery 中。这将允许您使用 .val() 函数在文本框为空的情况下检查和替换文本框的值
$(document).ready(function () {
if($('#id-of-your-textbox').val() == ' ')
{
$('#id-of-your-textbox').val('some text');
}
});
回答by Karan Kotabagi
Please try the below code, you need to set the value to the null first in the line
请尝试下面的代码,您需要在行中首先将值设置为 null
Name: <input type="text" id="myText" value=''>
Here is the demo :
这是演示:
function func() {
if (document.getElementById("myText").value == '') {
document.getElementById("myText").value = "Some text here";
} else {
alert("not null");
}
}
Name: <input type="text" id="myText" value="">
<p>Click the button to change the value of the text field.</p>
<input type="button" id="button" value="Click Me" onClick="func();" />