The logic works like this, taking into account that there are 3 users:
If user A is adding value, it will have to add the existing value of B and C to see what is left for it.
If it’s B, it’ll add up A and C.
And if it’s C, it’ll add A and B.
Now if you are a single user with three input fields we will only use JAVASCRIPT.
It just won’t work on Internet Explorer why I used input
of the kind number
to change the attribute MAX
real of each field:
function distribuirPercent(t){
var myform = document.forms.porcentagem;
var lmt;
var n = t.name;
/* Pré limitação por segurança */
myform.campo_a.max = myform.campo_a.value;
myform.campo_b.max = myform.campo_b.value;
myform.campo_c.max = myform.campo_c.value;
/* ---------------------------------------------*/
/* Transformar campos em números para operações */
var a = parseInt(myform.campo_a.value);
var b = parseInt(myform.campo_b.value);
var c = parseInt(myform.campo_c.value);
/* ---------------------------------------------*/
if(n == "campo_a"){
lmt = b+c;
}
if(n == "campo_b"){
lmt = a+c;
}
if(n == "campo_c"){
lmt = a+b;
}
console.clear();
console.log("Total dos outros 2: "+lmt);
lmt = 100-lmt;
console.log("Limite permitido: "+lmt);
t.max = lmt;
}
input[type=number] {
-webkit-appearance: textfield;
-moz-appearance: textfield;
appearance: textfield;
margin: 0;
border: solid 0px #fff;
width: 20px;
text-align: right;
}
<form name="porcentagem">
<label><input type="number" name="campo_a" min="0" max="100" value="0" onfocus="distribuirPercent(this)" size="3" />%</label><br />
<label><input type="number" name="campo_b" min="0" max="100" value="0" onfocus="distribuirPercent(this)" size="3" />%</label><br />
<label><input type="number" name="campo_c" min="0" max="100" value="0" onfocus="distribuirPercent(this)" size="3" />%</label>
</form>
With JS and HTML5, as you put it in the tags, it’s already possible. What you tried to do and what was the difficulty?
– Woss
I put only the input, leaving the numbers for the user to choose freely, but would like to put a mask to close properly at 100% in total. I know I can do with js, but I know very little about JS, I’m in the level "light bulb/ light bulb".
– waltersickmann