我需要制作一个积分计算器,用户将输入对象的数量,网站将告诉他们值(value)多少积分。到目前为止,我设法做到了这一点,但我想不出任何方法来阻止用户访问比现有对象更多的对象。这是代码:
<form>
<input type="text" name="ob1" size="3" value="0/24" onClick="this.value='';" /> object <input type="text" name="ob2" size="3" value="0/24" onClick="this.value='';" />
<br>
<input type="button" value="Calculate Points" onClick="document.getElementById('calcu').innerHTML='Supply points are ' + (ab1.value*5) +'. Tower points are '+(ab2.value*10)+'.'" name="clc"></form>
<div id="calcu"></div>
最佳答案
jsFiddle( http://jsfiddle.net/gP7SP/15/ )
我将继续进行一些假设,但现在开始:
<form>
Supply:<input type="text" id="supply" name="ob1" size="3" value="0/24" onClick="this.value='';" />
Tower:<input type="text" id="tower" name="ob2" size="3" value="0/24" onClick="this.value='';" />
<br/>
<input type="button" value="Calculate Points" onClick="calculate()" name="clc">
</form>
<div id="calcu"></div>
<script type="text/javascript">
function calculate()
{
// Retrieve the supply and tower values
var supply = parseInt( document.getElementById("supply").value );
var tower = parseInt( document.getElementById("tower").value );
// Make sure supply is less than 24
if ( supply > 24 )
{
alert( "Supply cannot be greater than 24" );
}
// Make sure tower is less than 24
else if ( tower > 24 )
{
alert( "Tower cannot be greater than 24" );
}
else
{
// Computer new values
supply = supply * 5;
tower = tower * 10;
// Write the values to the calcu div
document.getElementById("calcu").innerHTML = "Supply points: " + supply + ", Tower points: " + tower;
}
}
</script>
关于javascript - 当输入类型文本小于 0 或大于 24 时,如何禁用输入类型按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10306477/