Javascript 如果输入为空,则显示图片或其他
我有一张表格,像这样Javascript 如果输入为空,则显示图片或其他,javascript,html,Javascript,Html,我有一张表格,像这样 <form> <input type="text" id="abc" name="abc" value="abc"><img src="right.png"> <input type="text" id="abc1" name="abc" value=""><img src="wrong.png"> <input type="submit" id="submit" value="submit"> <
<form>
<input type="text" id="abc" name="abc" value="abc"><img src="right.png">
<input type="text" id="abc1" name="abc" value=""><img src="wrong.png">
<input type="submit" id="submit" value="submit">
</form>
但我不明白该怎么表现出来。当输入不为空时,如果输入为空,则将输入标记为所需:
<input type="text" required="required" minlength="1">
如果您必须支持某些似乎希望执行动态表单验证,请使用@divy3993。因此,您可以添加事件侦听器:
<input id="abc" name="abc" onchange="checkInput()">
但使用jQuery插件更好:
这可以在angularjs中使用ng show轻松完成
<!DOCTYPE html>
<html>
<head>
<title>Chat Application</title>
<script src="./scripts/angular.min.js"></script>
<link rel="stylesheet" type="text/css" href="./css/bootstrap.min.css">
</head>
<body ng-app="chatApp">
<div>
<input type="text" name="FirstName" ng-model="text" ng-init='text=""'><br>
<img src="right.png" alt="right" ng-show="text.length >0">
<img src="wrong.png" alt="wrong" ng-show="text.length === 0">
<input type="submit" id="submit" value="submit">
</div>
<script src="./scripts/app.js"></script>
</body>
</html>
聊天应用程序
0">
我会选择@dtanders,但使用JavaScript的简单解决方案不会对您造成伤害
函数myFunction(){
var x=document.getElementById(“abc”);
var curVal=x.值;
var imageRW=document.getElementById('img_right_error');
如果(曲线==“”)
{
//错了.png
imageRW.src=”http://findicons.com/files/icons/1671/simplicio/128/notification_error.png";
}
其他的
{
//right.png
imageRW.src=”https://d3n7l4wl5znnup.cloudfront.net/assets/images/icon-right.png";
}
}
您打算使用JQuery吗?根据输入显示不同结果的任何内容,如果为空,则显示错误,否则显示正确任何元素不能具有相同的id
。在您的情况下,两个input
元素具有相同的id=“abc”
。这是一个错误,我丢失了JavaScript代码(对于与此相关的另一个主题)。我没有任何代码用于此jQuery,对于此jQuery是overkill,带有插件的jQuery是way,way overkill。几乎正确,但是如果值已经填充,那么它也会显示交叉marks@ShuvoShuvo只需在页面加载时调用函数
在页面加载中。嗯..为什么这是被否决的?答案有错吗?
function checkInput() {
if ($("#abc").val().length == 0) {
// change image
}
}
<!DOCTYPE html>
<html>
<head>
<title>Chat Application</title>
<script src="./scripts/angular.min.js"></script>
<link rel="stylesheet" type="text/css" href="./css/bootstrap.min.css">
</head>
<body ng-app="chatApp">
<div>
<input type="text" name="FirstName" ng-model="text" ng-init='text=""'><br>
<img src="right.png" alt="right" ng-show="text.length >0">
<img src="wrong.png" alt="wrong" ng-show="text.length === 0">
<input type="submit" id="submit" value="submit">
</div>
<script src="./scripts/app.js"></script>
</body>
</html>