如何输入if条件,如果输入是给定的,当我们删除输入时, 所以应该显示任何东西。
我想把if语句,所以如果输入为空,那么应该显示"你必须写一些东西"
var number = document.getElementById("think");
var out = document.getElementById("output");
number.addEventListener("input", sol);
function sol(){
var one = parseInt(number.value);
var ans = ((one-12)/2)/5;
out.innerHTML = "this is what u have thinked in ur brain "+ans;
}
答案 0 :(得分:0)
使用isNan
检查parseInt
的结果是否为数字。
var number = document.getElementById("think");
var out = document.getElementById("output");
number.addEventListener("keyup", sol);
function sol(){
var one = parseInt(number.value);
if(isNaN(one))
{
out.innerHTML = "Yur brain sux";
}
else
{
var ans = ((one-12)/2)/5;
out.innerHTML = "this is what u have thinked in ur brain "+ans;
}
}
Whatcha thinkin bro: <input type="numeric" id="think"><br>
<div id="output"></div>
答案 1 :(得分:0)
另一种方法是在input元素中使用placeholder
属性。
<input type="text" name="name" placeholder="you have to write some thing" />