.value为什么没有获取到输入字段的值?

huangapple go评论76阅读模式
英文:

Why .value is not getting the value of the input field?

问题

我想要根据输入的值来更改 div 的边框半径。请问这里缺少什么?

HTML:

<div id="container">
  <p class="test">Test</p>
  <input id="value" type="number" />
  <input type="submit" id="submit" />
</div>

JS:

let btn = document.getElementById('submit');
let value = document.getElementById('value');
let border = document.getElementById("container").style.borderRadius;
let pixelVal = value.value + "px";

btn.addEventListener('click', function(){
  console.log(pixelVal);
  border = pixelVal;
})
英文:

I wanted to change the border radius of the div as the input value. can anybody please acknowledge me what is missing here.

HTML

&lt;div id=&quot;container&quot;&gt;
&lt;p class=&quot;test&quot;&gt;
Test
&lt;/p&gt;
&lt;input id=&quot;value&quot; type=&quot;number&quot; /&gt;
&lt;input type=&quot;submit&quot; id=&quot;submit&quot;/&gt;
&lt;/div&gt;

JS

let btn=document.getElementById(&#39;submit&#39;);
let value=document.getElementById(&#39;value&#39;);
let border=document.getElementById(&quot;container&quot;).style.borderRadius
let pixelVal=value.value+&quot;px&quot;;

btn.addEventListener(&#39;click&#39;, function(){
console.log(pixelVal);
	border=value+&quot;px&quot;
})

答案1

得分: 2

你只需要将像素计算和样式更改的代码行移到点击事件监听器的回调函数内部。

否则,这两行代码将只执行一次,而且是在点击监听器事件运行之前执行。

但是我们希望每次单击提交按钮时都应用该样式。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
let btn = document.getElementById('submit');
let value = document.getElementById('value');

btn.addEventListener('click', function() {
  let pixelVal = value.value + "px";
  console.log(pixelVal);
  document.getElementById("container").style.borderRadius = pixelVal;
})

<!-- language: lang-html -->
<div id="container">
  <p class="test">
    Test
  </p>

  <input id="value" type="number" />
  <input type="submit" id="submit" />
</div>

<!-- end snippet -->

希望对你有所帮助!

英文:

You just need to move the pixel calculation and style changing lines inside the click event listener's callback function.

Otherwise both of these code of lines will be executed only once, and that too before the click listener event runs.

But we want that style to be applied every-time we click submit button.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let btn = document.getElementById(&#39;submit&#39;);
let value = document.getElementById(&#39;value&#39;);

btn.addEventListener(&#39;click&#39;, function() {
  let pixelVal = value.value + &quot;px&quot;;
  console.log(pixelVal);
  document.getElementById(&quot;container&quot;).style.borderRadius = pixelVal;
})

<!-- language: lang-html -->

&lt;div id=&quot;container&quot;&gt;
  &lt;p class=&quot;test&quot;&gt;
    Test
  &lt;/p&gt;

  &lt;input id=&quot;value&quot; type=&quot;number&quot; /&gt;
  &lt;input type=&quot;submit&quot; id=&quot;submit&quot; /&gt;
&lt;/div&gt;

<!-- end snippet -->

Hope it helps!

答案2

得分: 1

let btn=document.getElementById('submit');
let value=document.getElementById('value');
let container=document.getElementById("container");

btn.addEventListener('click', function(){
container.style.borderRadius = value.value + 'px';
})

#container {
padding: 10px;
border: 1px solid #000;
}

Test


英文:

Take a look at this code for orientation.

The input value is read in the click event:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let btn=document.getElementById(&#39;submit&#39;);
let value=document.getElementById(&#39;value&#39;);
let container=document.getElementById(&quot;container&quot;);

btn.addEventListener(&#39;click&#39;, function(){
  container.style.borderRadius = value.value + &#39;px&#39;;
})

<!-- language: lang-css -->

#container {
padding: 10px;
border: 1px solid #000;
}

<!-- language: lang-html -->

&lt;div id=&quot;container&quot;&gt;
&lt;p class=&quot;test&quot;&gt;
Test
&lt;/p&gt;
&lt;input id=&quot;value&quot; type=&quot;number&quot; /&gt;
&lt;input type=&quot;submit&quot; id=&quot;submit&quot;/&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

  1. pixelVal 被移至事件监听器内部。这确保了在单击按钮时读取输入的值,而不是在脚本最初加载时。

  2. 在事件监听器中访问容器元素的样式属性。

英文:
  1. pixelVal is moved inside the event listener. This ensures that the value of the input is read when the button is clicked and not when the script is initially loaded.

  2. Access the style property of the container element in the event listener.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let btn = document.getElementById(&#39;submit&#39;);
let value = document.getElementById(&#39;value&#39;);
let border = document.getElementById(&quot;container&quot;);

btn.addEventListener(&#39;click&#39;, function(){
    let pixelVal = value.value+&quot;px&quot;;
    console.log(pixelVal);
    border.style.borderRadius = pixelVal;
})

<!-- language: lang-css -->

#container {
  border: 1px solid;
  padding: 20px;
}

<!-- language: lang-html -->

&lt;div id=&quot;container&quot;&gt;
  &lt;p class=&quot;test&quot;&gt; Test&lt;/p&gt;
  &lt;input id=&quot;value&quot; type=&quot;number&quot; /&gt;
  &lt;input type=&quot;submit&quot; id=&quot;submit&quot;/&gt;
&lt;/div&gt;

<!-- end snippet -->

答案4

得分: 0







Document


Test




英文:
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;title&gt;Document&lt;/title&gt;
    &lt;style&gt;
      #container{
        background-color: yellow;
        border: 1px solid black;
      }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;container&quot;&gt;
      &lt;p class=&quot;test&quot;&gt;Test&lt;/p&gt;
      &lt;input id=&quot;value&quot; type=&quot;number&quot; /&gt;
      &lt;input type=&quot;submit&quot; id=&quot;submit&quot; /&gt;
    &lt;/div&gt;
  &lt;/body&gt;
  &lt;script&gt;
    let btn = document.getElementById(&quot;submit&quot;);
    btn.addEventListener(&quot;click&quot;, function () {
      let value = document.getElementById(&quot;value&quot;);
      let border = document.getElementById(&quot;container&quot;);
      let pixelVal = value.value + &quot;px&quot;;
      console.log(pixelVal);
      border.style.borderRadius = pixelVal
    });
  &lt;/script&gt;
&lt;/html&gt;
  • Here I have improved your code so now its working. Input value should
    be only fetched when button is clicked otherwise It will be null. Thats
    why Your code is not working as you want.
  • Let me know if you don't get...Thank You...!

huangapple
  • 本文由 发表于 2023年6月19日 18:54:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定