简体   繁体   English

有没有办法从 HTML 中的 function 调用中访问 this.value (输入)

[英]Is there a way to access this.value (of an input) from within a function call in HTML

I have an HTML input for which I want to limit the possible to 100 when manually entering, I have a min and a max so that if they use the spinner (up and down arrows) it limits it already.我有一个 HTML 输入,我想在手动输入时将其限制为 100,我有一个最小值和一个最大值,因此如果他们使用微调器(向上和向下箭头)它已经限制了它。 To do this, instead of using Inline Javascript, I wanted to call a function that could be used for several functions throughout my HTML file.为此,我想调用一个 function,而不是使用内联 Javascript,它可用于整个 HTML 文件中的多个功能。

Wanted Functionality:想要的功能:

<input id="..." oninput="foo()">
...
<script>
function foo() {
   if(this.value > 100) {
      this.value = 100;    // If the inputted value is 99, do nothing, if it's 999, change it to 100
   }
}
</script> 

I wanted a functionality like this, which would let me this this.value - however - this.value is not accessible (I think???) from within the foo function.我想要一个这样的功能,它可以让我这个 this.value - 但是 - this.value 无法从 foo function 中访问(我认为???)。

So I tried doing this:所以我试着这样做:

<input id="..." oninput="foo(this.value)">
...
<script>
function foo(value) {
   if(value > 100) {
      value = 100;
   }
}
</script>

But I think only the "copy" of value gets changed to 100, not the actual value.但我认为只有值的“副本”更改为 100,而不是实际值。 So I was able to do it by returning a value like this:所以我可以通过返回这样的值来做到这一点:

<input id="..." oninput="this.value = foo(this.value)">
...
<script>
function foo(value) {
   if(value > 100) {
      value = 100;
   }
   return value;
}
</script>

but this code seems quite clumsy, is there a better way to do this?但是这段代码看起来很笨拙,有没有更好的方法来做到这一点?

EDIT : SOLVED, Thank you all for the answers, this is my code now.编辑:已解决,谢谢大家的回答,这是我现在的代码。 I also parse the input to make sure that it's ONLY numbers.我还解析输入以确保它只是数字。 no "e" or "."没有“e”或“.” because all I care about are integers.因为我只关心整数。

<input id="area-level-input" type="number" value="1" min="1" max="100" oninput="limitInput.call(this, 100)" onkeydown="isValidNumberEntry()">

Where the respective functions are:其中各自的功能是:

function limitInput(limit) {
    if(this.value > limit) {
        this.value = limit;
    }
}


function isValidNumberEntry() {
    // keyCode 8: Backspace; keyCode 46: Delete
    return (event.keyCode === 8 || event.keyCode === 46) ? true : !isNaN(Number(event.key));
}

You can use the call method :您可以使用call方法

<input id="..." oninput="foo.call(this)">
...
<script>
function foo() {
   if(this.value > 100) {
      this.value = 100;    // If the inputted value is 99, do nothing, if it's 999, change it to 100
   }
}
</script> 

You have two ways to accomplish what you are trying:你有两种方法来完成你正在尝试的事情:

  • Passing this to the inline function将此传递给内联 function
  • Passing event to the inline function事件传递给内联 function

Both have the same outcome and will function as you'd expect:两者都有相同的结果,并且 function 如您所料:

<input oninput="foo(this)"/>

or或者

<input oninput="foo(event)"/>

then your function can be:那么你的 function 可以是:

function foo(thisChoice) {

  if(thisChoice.value > 100) {
      thisChoice.value = 100;
   }

}

or或者

function foo(eventChoice) {

  if(eventChoice.target.value > 100) {
      eventChoice.target.value = 100;
   }

}

Bear in mind that one you are passing only the input tag, and the other you are passing the entire Event to the function.请记住,一个您只传递输入标签,另一个您将整个事件传递给 function。

Your approach is correct.你的方法是正确的。

You could modify the value of the element directly in the function to make it a bit more readable and reusable.您可以直接在 function 中修改元素的值,使其更具可读性和可重用性。 For example:例如:

<input type="number" min="0" max="100" oninput="limitTo(100, this)" />
<script>
  function limitTo(upperLimit, el) {
    if (el.value > upperLimit) {
      el.value = upperLimit;
    }
  }
</script>

I also suggest to set the type attribute of the input element to number .我还建议将 input 元素的type属性设置为number The advantage is that the browser will make sure to accept only digits, and it allows to set the attribute max .优点是浏览器将确保只接受数字,并且允许设置属性max

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

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