简体   繁体   English

JavaScript 代码不执行,尽管“else if”条件满足

[英]JavaScript code does not execute, although the "else if" condition is fulfilled

I am writing a HTML page with some JavaScript, and I'm meeting with a problem.我正在用一些 JavaScript 写一个 HTML 页面,我遇到了一个问题。

I need that when a input is changed, it checks if its value is greater or equals to another input's value, but I can't find it working, although I use the same exact code for that other input (and it's working), but with changing the variables.我需要在更改输入时检查其值是否大于或等于另一个输入的值,但我找不到它工作,尽管我对其他输入使用相同的确切代码(并且它正在工作),但是随着变量的改变。

 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;&emsp; <input type="text" name="wlGlobalMaxClients" id="wlGlobalMaxClients" maxlength="3" size="1" value="64" onchange="changeGlobalMaxClients()">&emsp: <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:&emsp. <input type="text" name="wlMaxClients" id="wlMaxClients" maxlength="3" size="1" value="32" onchange="changeMaxClients()">&emsp; <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 -->

If run the code and change the value of globalMaxClients to a value that is less or equals to maxClients , it shows up the alert and refreshes the page, but if I try to change the value of maxClients to a value that is greater or equals to globalMaxClients , it does not show up the alert nor it refreshes the page.如果运行代码并将globalMaxClients的值更改为小于或等于maxClients的值,它会显示警报并刷新页面,但如果我尝试将maxClients的值更改为大于或等于的值globalMaxClients ,它既不显示警报也不刷新页面。

I tried to switch the maxClients >= globalMaxClients part to globalMaxClients <= maxClients , but it doesn't work.我试图将maxClients >= globalMaxClients部分切换为globalMaxClients <= maxClients ,但它不起作用。

If more information is needed, just reply to the post.如果需要更多信息,只需回复帖子即可。 Sorry if there's any grammatical error, I'm not too good with English.对不起,如果有任何语法错误,我的英语不太好。

EDIT/UPDATE : my problem is that if maxClients value is greater or equals to globalMaxClients value (or if globalMaxClients value is less or equals to maxClients value), it should show an alert and refresh the page, but I found out that if the value of any of the two variables is more than 100 (including 100 ), the alert is not shown and it does not refresh the page.编辑/更新:我的问题是,如果maxClients值大于或等于globalMaxClients值(或者如果globalMaxClients值小于或等于maxClients值),它应该显示警报并刷新页面,但我发现如果该值两个变量中的任何一个大于100 (包括100 ),不显示警报并且不刷新页面。 However, if the values are below 100 , the code does work.但是,如果值低于100 ,代码就可以工作。

EDIT 2 : also, it seems that if I put a value higher than 100 on globalMaxClients , the code works, but at the contrary direction.编辑 2 :另外,如果我在globalMaxClients上设置一个高于100的值,代码会起作用,但方向相反。 The thing that happens is:发生的事情是:

If I change the value of globalMaxClients to, for example, 112 , and the value of maxClients is below this number (let's say 10 ), it shows the alert saying that globalMaxClients value cannot be less or equal to maxClients , but the thing is that 112 > 10 , so it should not show that alert.例如,如果我将globalMaxClients的值更改为112 ,并且maxClients的值低于此数字(比方说10 ),它会显示警告说globalMaxClients值不能小于或等于maxClients ,但问题是112 > 10 ,所以它不应该显示那个警报。

It's not entirely clear what you are trying to do.尚不完全清楚您要做什么。 If you want to be able to test and change values the code below should work for you.如果您希望能够测试和更改值,下面的代码应该适合您。 If you want to always set the values back to the defaults uncomment both loads如果您想始终将值设置回默认值,请取消注释这两个负载

 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;&emsp; <input type="text" name="wlGlobalMaxClients" id="wlGlobalMaxClients" maxlength="3" size="1" value="64" onchange="changeGlobalMaxClients()">&emsp: <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:&emsp. <input type="text" name="wlMaxClients" id="wlMaxClients" maxlength="3" size="1" value="32" onchange="changeMaxClients()">&emsp; <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 -->

Finally I got it working.最后我让它工作了。

The problem was, as stated by Poul Bak , my code did not have parseInt(document.getElementById("id-of-element").value) on every var code.问题是,正如Poul Bak所述,我的代码在每个var代码上都没有parseInt(document.getElementById("id-of-element").value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM