我正在尝试编写一个小的javascript来验证我的网站形式的type =“number”的输入。基本上,这个函数试图确保允许的最大输入长度不超过我传入的参数。我编写了函数来接受两个参数,一个是html元素,另一个是maxlength。但是,在我将此函数添加到input元素后,它似乎不起作用。请看一看,告诉我哪里出错了,错过了哪些概念。
/* Show submit button only when the form has been validated */
/* Variables */
var form = document.querySelector(".resForm");
var resFormBtn = document.querySelector(".submitButton");
var numInput = document.querySelector("#numberInput");
function showSubmitButton (){
if (form.checkValidity() === true){
resFormBtn.style.display = "block";
}
}
function validateNumInput (maxlength,ele){
if (ele.length > maxlength){
ele.value = ele.value.slice(0,maxlength);
}
}
form.addEventListener("change",showSubmitButton,false);
numInput.addEventListener("input",function(){
validateNumInput(8, numInput);
},false);
答案 0 :(得分:2)
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from time import sleep
%matplotlib inline
for i in range(1,3):
PATH = "kodim01.png"
N = "%02d" % i
print PATH.replace("01", N)
image = mpimg.imread(PATH.replace("01", N))
# plt.show()
plt.imshow(image)
sleep(5) #in seconds
这里有问题。 ele是一个引用的节点,它没有这样一个名为“length”的属性,因此条件中的代码永远不会触发。
只需将其更改为
即可if (ele.length > maxlength)
代码就可以了。