[英]JavaScript toUpperCase() not working
I'm trying to make a small script that determines whether inputted text is entirely uppercase, entirely lowercase, or neither. 我正在尝试制作一个小的脚本,确定输入的文本是完全大写,完全小写还是都不是。 Here:
这里:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script>
function caps (p1){
console.log ("yes");
if (p1.toUpperCase() == p1){
alert ("Is uppercase")
}
else if (p1.toLowerCase() == p1){
alert ("Is lowercase")
}
else {
alert ("Is mix of both");
}
}
</script>
<form id="form1" name="form1" method="post" action="">
<p>
<label>Write something<br />
<input type="text" name="numero" id="numero" />
</label>
</p>
<p>
<input onclick="caps(numero)" type="submit" name="button" id="button" value="Submit" />
</p>
</form>
</body>
</html>
However, it doesn't work; 但是,它不起作用。 the Firefox Web Console insists that "toUpperCase()" is not a valid method.
Firefox Web Console坚持认为“ toUpperCase()”不是有效方法。 What's happening here?
这里发生了什么事?
In your onclick
, you call the function like this: caps(numero)
在
onclick
,您可以这样调用函数: caps(numero)
This isn't passing numero
as a String
. 这没有将
numero
作为String
传递。 Its passing it as a variable name. 它通过它作为变量名。 An undefined one.
一个未定义的。
.toUpperCase
is a method on String
, and will not exist on undefined
. .toUpperCase
是String
上的方法,在undefined
上将不存在。
Instead, try: caps('numero')
而是尝试:
caps('numero')
Or, if you're trying to pass the value of your text input: 或者,如果您尝试传递文本输入的值 :
caps(document.getElementById('numero').value)
Perhaps better yet, build that into your function: 也许更好,将其内置到您的函数中:
function caps (id) {
var p1 = document.getElementById(id).value
if (p1.toUpperCase() == p1){
alert ("Is uppercase")
}
else if (p1.toLowerCase() == p1){
alert ("Is lowercase")
}
else {
alert ("Is mix of both");
}
}
That way, you can call it like onclick="caps('numero')"
, passing in the id
of the input whose value you want to UpperCase 这样,您可以像
onclick="caps('numero')"
一样调用它,将您想要输入其值的输入的id
传递给UpperCase
Try this way, p1 is input element not your input element's value so compare with p1.value 尝试这种方式,p1是输入元素而不是输入元素的值,因此请与p1.value进行比较
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function caps (p1)
{
console.log (p1); //see here
console.log (p1.value);
if (p1.value.toUpperCase() == p1.value){
alert ("Is uppercase")
}
else if (p1.value.toLowerCase() == p1.value){
alert ("Is lowercase")
}
else {
alert ("Is mix of both");
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label>Write something<br />
<input type="text" name="numero" id="numero" />
</label>
</p>
<p>
<input onclick="caps(numero)" type="submit" name="button" id="button" value="Submit" />
</p>
</form>
</body>
</html>
not p1.toUpperCase() == p1
, p1 stand for input
dom element where id="p1", 不是
p1.toUpperCase() == p1
,p1代表input
dom元素,其中id =“ p1”,
don't have touppercase
method,only string has this method,so you should use p1.value 没有
touppercase
方法,只有string有此方法,因此您应该使用p1.value
document.getElementById(p1)
equals to p1
, p1
is the id name document.getElementById(p1)
等于p1
, p1
是id名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.