如果输入值为空,则使用 Javascript 分配“空”值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6752714/
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
If input value is blank, assign a value of "empty" with Javascript
提问by Maverick
So I have an input field, if it's blank, I want its value to be the words "empty", but if there is any value inputted, I want the value to be the inputted value. I want to use javascript for this, any idea how this can be done?
所以我有一个输入字段,如果它是空白的,我希望它的值是“空”这个词,但是如果输入了任何值,我希望该值是输入的值。我想为此使用 javascript,知道如何做到这一点吗?
UPDATE:Sorry, I don't think I explained it too well. I don't mean placeholder text. I mean the captured value of it. So if it's blank, the captured val() for it should be "empty", if it's filled, the captured val() for it should be that val()
更新:对不起,我认为我解释得不太好。我的意思不是占位符文本。我的意思是它的捕获价值。所以如果它是空白的,它的捕获 val() 应该是“空的”,如果它被填充了,它的捕获 val() 应该是那个 val()
回答by Jamie Dixon
If you're using pure JS you can simply do it like:
如果您使用纯 JS,您可以简单地这样做:
var input = document.getElementById('myInput');
if(input.value.length == 0)
input.value = "Empty";
Here's a demo: http://jsfiddle.net/nYtm8/
这是一个演示:http: //jsfiddle.net/nYtm8/
回答by Jason Gennaro
I'm guessing this is what you want...
我猜这就是你想要的......
When the form is submitted, check if the value is empty and if so, send a value = empty.
提交表单时,检查值是否为空,如果是,则发送值 = 空。
If so, you could do the following with jQuery.
如果是这样,您可以使用 jQuery 执行以下操作。
$('form').submit(function(){
var input = $('#test').val();
if(input == ''){
$('#test').val('empty');
}
});
HTML
HTML
<form>
<input id="test" type="text" />
</form>
http://jsfiddle.net/jasongennaro/NS6Ca/
http://jsfiddle.net/jasonennaro/NS6Ca/
Click your cursor in the box and then hit enter to see the form submit the value.
在框中单击光标,然后按 Enter 以查看表单提交值。
回答by Varun Achar
回答by Daniel
You can set a callback function for the onSubmit event of the form and check the contents of each field. If it contains nothing you can then fill it with the string "empty":
您可以为表单的 onSubmit 事件设置回调函数,并检查每个字段的内容。如果它不包含任何内容,则可以用字符串“空”填充它:
<form name="my_form" action="validate.php" onsubmit="check()">
<input type="text" name="text1" />
<input type="submit" value="submit" />
</form>
and in your js:
在你的 js 中:
function check() {
if(document.forms["my_form"]["text1"].value == "")
document.forms["my_form"]["text1"].value = "empty";
}
回答by katspaugh
You can do this:
你可以这样做:
var getValue = function (input, defaultValue) {
return input.value || defaultValue;
};
回答by Wackyedd
I think the easiest way to solve this issue, if you only need it on 1 or 2 boxes is by using the HTML input's "onsubmit" function.
我认为解决这个问题的最简单方法是使用 HTML 输入的“onsubmit”功能,如果您只需要在 1 或 2 个盒子上使用它。
Check this out and i will explain what it does:
看看这个,我将解释它的作用:
<input type="text" name="val" onsubmit="if(this == ''){$this.val('empty');}" />
So we have created the HTML text box, assigned it a name ("val" in this case) and then we added the onsubmitcode, this code checks to see if the current input box is empty and if it is, then, upon form submit it will fill it with the words empty.
因此,我们创建了 HTML 文本框,为其分配了一个名称(在本例中为“val”),然后我们添加了onsubmit代码,此代码检查当前输入框是否为空,如果为空,则在表单上提交它将用空词填充它。
Please note, this code should also function perfectly when using the HTML "Placeholder" tag as the placeholder tag doesn't actually assign a value to the input box.
请注意,当使用 HTML“Placeholder”标签时,此代码也应该可以完美运行,因为占位符标签实际上并未为输入框分配值。