[英]Can I call JS function and how?
Okey community,奥基社区,
function validate() { var name = document.getElementById("name").value; var p = document.getElementById("pname"); console.log("validating"); // added by PM-77-1 if (name === null) { p.className += " show"; return false; } else { return true; } }
.split { height: 100%; position: fixed; top: 0; } .left { left: 0; width: 40%; } .right { right: 0; width: 60%; } .form { margin: 30% 5% 30% 5%; } .input { margin: 2% 2% 2% 2%; height: 40px; width: 35%; } .lower { width: 76%; } h6 { background-color: #3399ff; margin-top: 0px; min-height: 50px; max-height: 50px; font-size: 32px; color: white; } button { background-color: #3399ff; color: white; width: 77%; min-height: 50px; border: none; border-radius: 5px; margin-top: 10%; } body { font-family: Futura Md BT; } #map_canvas { height: 100%; } .name { display: none; } .lastname { display: none; } .address { display: none; } .city { display: none; } .country { display: none; } .show { display: block; margin-top: 0px; margin-bottom: 0px; color: red; }
<div class="split left"> <center> <h6>User Details</h6> <div class="form"> <form action="input.php" method="POST" onsubmit="event.preventDefault(); validate()"> <p class="name" id="pname">Please enter First Name</p> <p class="lastname" id="lastname">Please enter Last Name</p> <p class="address" id="address">Please enter Street / Number</p> <p class="city" id="city">Please enter City</p> <p class="country" id="country">Please enter country</p> <input type="text" class="input" id="name" name="firstname" placeholder="First Name"> <input type="text" class="input" id="lastname" name="lastname" placeholder="Last Name"><br> <input type="text" class="input lower" id="address" name="streetnumber" placeholder="Street / Number"><br> <input type="text" class="input lower" id="city" name="city" placeholder="City"><br> <input type="text" class="input lower" id="country" name="country" placeholder="Country"><br> <button type="submit" onclick="return validate()">Add User</button> </form> </div> </center> </div>
Please test this and tell me why is it not working.请测试这个并告诉我为什么它不起作用。 I tried everything, but somehow my js is not triggering.
我尝试了一切,但不知何故我的 js 没有触发。 I changed ids and everything.
我改变了ID和一切。 Plus tell me if I did right thing in the form with validating default because I am doing that for the first time.
另外告诉我我是否在验证默认值的表单中做了正确的事情,因为我是第一次这样做。 Thank you everyone, so much.
谢谢大家,非常感谢。
I use body onload (to start a google map) but I did not paste that part, it works fine.我使用 body onload(启动谷歌地图)但我没有粘贴那部分,它工作正常。
Please help me with an example, Thank you so much community !!!请帮我举个例子,非常感谢社区!!!
onclick
of the form, you are putting return functionCall()
when its just necessary the function.onclick
表单时,您将 return functionCall()
在它刚好需要该函数时放置。 without the word return.function validate() {
console.log("eslloooooooo")
var name = document.querySelector("#name").value;
var p = document.querySelector("#pname");
if (name.innerHTML === '') {
p.classList.add("show");
}
}
I recommend it cause first, you can use a
querySelector
that uses CSS selectors and also you can use the option classList of your node to add a class to your item.我首先推荐它,因为您可以使用使用 CSS 选择器的
querySelector
,也可以使用节点的选项 classList 向项目添加类。
Hope this helps you希望这对你有帮助
Your function validate
is calling just fine, but some statements inside it are not executing quite well.您的函数
validate
调用得很好,但其中的一些语句执行得不太好。 A few issues here:这里有几个问题:
1- To add a class to the element p
, use p.classList.add("className")
1- 要将类添加到元素
p
,请使用p.classList.add("className")
2- When users leave the name field blank, the value is an empty string ( ''
), not null. 2- 当用户将名称字段留空时,该值为空字符串 (
''
),而不是 null。 So, instead of checking:所以,而不是检查:
if (name === null)
Check instead for:改为检查:
if(name == '')
//or, even shorter:
if(!name)
Therefore, the JS becomes:因此,JS变成:
function validate() {
var name = document.getElementById("name").value;
var p = document.getElementById("pname");
console.log("validating");
if (!name) {
p.classList.add( "show");
return false;
} else {
return true;
}
}
onclick
shouldn't be returning anything, so you can remove the ()
and just have the function name onclick
不应返回任何内容,因此您可以删除()
并获得函数名称
<button type="submit" onclick="return validate()">Add User</button>
<!-- Should be -->
<button type="submit" onclick="validate">Add User</button>
Also, you're checking for null
when you should be checking for an empty string此外,当您应该检查空字符串时,您正在检查
null
if (name === null) { ... }
// Should be
if (name === '') { ... }
当您应该调用 validate() 函数时,看起来您是在主体加载(在您的 HTML 中)上调用 initialize() 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.