我正在用一些JavaScript编写HTML页面,但是遇到了问题。
我需要在更改输入时检查它的值是否大于或等于另一个输入的值,但是我找不到它,尽管我为那个其他输入使用了相同的精确代码(并且它正在工作),但更改了变量。
function changeGlobalMaxClients() {
var maxClients = document.getElementById("wlMaxClients").value;
var globalMaxClients = document.getElementById("wlGlobalMaxClients").value;
if (globalMaxClients.length == "0" || globalMaxClients == "0") {
alert("Global Max Clients setting cannot be disabled.");
location.reload();
} else if (maxClients >= globalMaxClients) {
alert("Global Max Clients value cannot be less nor equal to Max Clients setting.");
location.reload();
};
};
function changeMaxClients() {
var maxClients = document.getElementById("wlMaxClients").value;
var globalMaxClients = document.getElementById("wlGlobalMaxClients").value;
if (maxClients.length == "0" || maxClients == "0") {
alert("Max Clients setting cannot be disabled.");
location.reload();
} else if (maxClients >= globalMaxClients) {
alert("Max Clients value cannot be greater nor equal to Global Max Clients setting.");
location.reload();
};
};
<!-- Global Max Clients -->
<p>Global Max Clients: 
<input type="text" name="wlGlobalMaxClients" id="wlGlobalMaxClients" maxlength="3" size="1" value="64" onchange="changeGlobalMaxClients()"> 
<span class="small">Note: this setting cannot be less nor equal to the value of Max Clients setting.</span>
</p>
<br>
<!-- END Global Max Clients -->
<!-- Max Clients -->
<p>Max Clients: 
<input type="text" name="wlMaxClients" id="wlMaxClients" maxlength="3" size="1" value="32" onchange="changeMaxClients()"> 
<span class="small">Note: this setting cannot be greater nor equal to the value of Global Max Clients setting.</span>
</p>
<br>
<!-- END Max Clients -->
如果运行代码并将globalMaxClients
的值更改为小于或等于maxClients
的值,它将显示警报并刷新页面,但是如果我尝试更改该值maxClients
的值大于或等于globalMaxClients
,则不会显示警报,也不会刷新页面。
我尝试将maxClients >= globalMaxClients
部分切换为globalMaxClients <= maxClients
,但是它不起作用。
如果需要更多信息,只需回复该帖子。抱歉,如果有语法错误,我对英语不太满意。
编辑/更新:我的问题是,如果maxClients
的值大于或等于globalMaxClients
的值(或者如果globalMaxClients
的值小于或等于maxClients
值),它应该显示警报并刷新页面,但是我发现,如果两个变量中的任何一个的值大于100
(包括100
),则警报不会显示,并且不会刷新页面。但是,如果值小于100
,则代码可以正常工作。
编辑2 :同样,如果我在100
上输入一个高于globalMaxClients
的值,代码会起作用,但方向相反。发生的事情是:
如果我将globalMaxClients
的值更改为例如112
,而maxClients
的值低于此数字(例如10
),则会显示警告说globalMaxClients
的值不能小于或等于maxClients
,但实际上是112 > 10
,因此它不应显示该警告。
答案 0 :(得分:0)
目前尚不清楚您要做什么。如果您希望能够测试和更改值,则下面的代码将为您工作。如果您想始终将值设置回默认值,请取消注释两个加载
var globalMaxClients = document.getElementById("wlGlobalMaxClients").value;
var maxClients = document.getElementById("wlMaxClients").value;
function changeGlobalMaxClients() {
var maxClients = document.getElementById("wlMaxClients").value;
var globalMaxClients = document.getElementById("wlGlobalMaxClients").value;
console.log(globalMaxClients,maxClients);
if (globalMaxClients.length == "0" || globalMaxClients == "0") {
alert("Global Max Clients setting cannot be disabled.");
//location.reload();
} else if (maxClients >= globalMaxClients) {
console.log('hello');
alert("Global Max Clients value cannot be less nor equal to Max Clients setting.");
//location.reload();
};
};
function changeMaxClients() {
console.log('in');
var maxClients = document.getElementById("wlMaxClients").value;
var globalMaxClients = document.getElementById("wlGlobalMaxClients").value;
console.log(globalMaxClients,maxClients);
if (maxClients.length == "0" || maxClients == "0") {
alert("Max Clients setting cannot be disabled.");
//location.reload();
} else if (maxClients >= globalMaxClients) {
alert("Max Clients value cannot be greater nor equal to Global Max Clients setting.");
//location.reload();
};
};
<!-- Global Max Clients -->
<p>Global Max Clients: 
<input type="text" name="wlGlobalMaxClients" id="wlGlobalMaxClients" maxlength="3" size="1" value="64" onchange="changeGlobalMaxClients()"> 
<span class="small">Note: this setting cannot be less nor equal to the value of Max Clients setting.</span>
</p>
<br>
<!-- END Global Max Clients -->
<!-- Max Clients -->
<p>Max Clients: 
<input type="text" name="wlMaxClients" id="wlMaxClients" maxlength="3" size="1" value="32" onchange="changeMaxClients()"> 
<span class="small">Note: this setting cannot be greater nor equal to the value of Global Max Clients setting.</span>
</p>
<br>
<!-- END Max Clients -->
答案 1 :(得分:0)
最后我开始工作了。
问题是,如Poul Bak所述,我的代码在每个parseInt(document.getElementById("id-of-element").value)
代码上都没有var
。