程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了考虑一个输入字段为 90% 并自动显示另一个 10%大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决虑一个输入字段为 90% 并自动显示另一个 10%?

开发过程中遇到虑一个输入字段为 90% 并自动显示另一个 10%的问题如何解决?下面主要结合日常开发的经验,给出你关于虑一个输入字段为 90% 并自动显示另一个 10%的解决方法建议,希望对你解决虑一个输入字段为 90% 并自动显示另一个 10%有所启发或帮助;

我有两个输入值,这两个值应该等于 100%,一个输入包含 90%,另一个应该包含 10%,该值应该根据任何字段中的输入变化自动计算。

function checkValue (typE) {
  var no = 0;
  var percent = 0;
  var otherPercent = 0;
  if (type === 'ninetyPercent') {
    no = document.getElementByID('ninetyPercent').value;
    percent = parseInt(no) / 90;
    otherPercent = (parsefloat(percent.toFixed(2)) - parseInt(no))
    document.getElementByID('tenPercent').value = otherPercent;
  } else {
    no = document.getElementByID('tenPercent').value;
    percent = parseInt(no) / 10;
    otherPercent = (parsefloat(percent.toFixed(2)) + parseInt(no));
    document.getElementByID('ninetyPercent').value = otherPercent;
  }
}
<script src="https://cdnjs.cloudFlare.com/AJAX/libs/jquery/3.3.1/jquery.min.Js"></script>
<label>This should consIDer as 90% for the input</label>
<input type="number" ID="ninetyPercent" oninput="checkValue('ninetyPercent')" placeholder="90%">
<br>
<br>
<label>This should consIDer as 10% for the input</label>
<input type="number" ID="tenPercent" oninput="checkValue('tenPercent')" placeholder="10%">

一般的想法是,如果我们在第一个输入中插入 90,第二个输入应该自动填充 10,同样如果我们在第二个输入中输入 10,第一个输入应该自动填充 90,以此类推。

提前致谢

解决方法

这是我的方法:

const inputs = document.querySELEctorAll('#foo input');

function getOtherInput(changedInput) {
  return [...inputs].find(input => input !== changedInput);
}

foo.addEventListener('input',(event) => {
  @R_674_5276@hangedInput = event.target;
  const definingValue = number(changedInput.value);
  const definingPercentage = number(changedInput.dataset.percentagE);
  
  const derivedInput = getOtherInput(changedInput);
  const derivedPercentage = number(derivedInput.dataset.percentagE);
  const derivedValue = definingValue / definingPercentage * derivedPercentage;
  derivedInput.value = derivedValue;
});

bar.addEventListener('click',() => {
  let firstPercentage = NaN;
  while (isNaN(parseInt(firstPercentagE))) {
    firstPercentage = parseInt(prompt('Enter the percentage for the first input (less than 100,greater than 0,Integer):'));
  }
  inputs[0].dataset.percentage = firstPercentage;
  inputs[1].dataset.percentage = 100 - firstPercentage;
})
<div id="foo">
  <label>This should consider as 90% for the input
<input type="number" data-percentage="90" placeholder="90%"></label>
  <br>
  <br>
  <label>This should consider as 10% for the input
<input type="number" data-percentage="10" placeholder="10%"></label>
</div>

<button type="button" id="bar">Change ratio</button>

当您单击标有“更改比率”的按钮时,它只会更改 data-percentage 属性,并且元素行为会自动更新(仍然需要您在任一输入中触发输入事件以进行更新)。>

大佬总结

以上是大佬教程为你收集整理的考虑一个输入字段为 90% 并自动显示另一个 10%全部内容,希望文章能够帮你解决考虑一个输入字段为 90% 并自动显示另一个 10%所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:10%90%