我是Javascript的初学者,并且遇到了以下问题,但仍未按预期工作,我想向社群寻求帮助。基本上,我有一个(不确定,在运行时建立起来,这里仅为简单2)数量(数字)输入字段,我想验证输入的值是否在0到10之间。
我只能使用纯JavaScript和JavaScript函数的“按钮”定义。感谢任何评论!
<p> Please enter a value 0 and 10. </p>
<input class="offers" type="number" step="0.01" id='1'>
<p> Please enter another value between 0 and 10. </p>
<input class="offers" type="number" step="0.01" id='2'>
<input type="submit" name="OutterButton" id="absenden" style="display: none">
<input type="button" value="Absenden" onclick="function testOffer() {
var offers = document.getElementsByClassName('offers');
for (var i = 0; i < offers.length; i++){
var offer = document.getElementById(offers[i]).value;
if (offer>=10 != offer<=0)
{ alert('Wrong Number'); }
else if ( isNaN(offer) )
{ alert('Please enter a number).'); }
}
}
else{var subButton = document.getElementsByName('OutterButton')[0];
subButton.style.display='inline'; subButton.click();
subButton.style.display='none'}}; testOffer();">
答案 0 :(得分:1)
为什么不使用<script>
代码?
<script>
function testOffer() {
var offers = document.getElementsByClassName('offers');
for (var i = 0; i < offers.length; i++){
var offer = document.getElementById(offers[i]).value;
if (offer>=10 != offer<=0)
{ alert('Wrong Number'); }
else if ( isNaN(offer) )
{ alert('Please enter a number).'); }
}
} ...
</script>
<input type="button" value="Absenden" onclick="testOffer()" />
是的,您的Javascript中存在问题。因为它是嵌入式的,所以很难调试。但问题出在这里:
function testOffer() {
var offers = document.getElementsByClassName('offers');
for (var i = 0; i < offers.length; i++){
var offer = document.getElementById(offers[i]).value;
if (offer>=10 != offer<=0)
{ alert('Wrong Number'); }
else if ( isNaN(offer) )
{ alert('Please enter a number).'); }
}
}
else{var subButton = document.getElementsByName('OutterButton')[0];
subButton.style.display='inline'; subButton.click();
subButton.style.display='none'}}; testOffer();
如您所见,else
声明来自任何地方。函数定义在else
。