简体   繁体   English

当我使用按钮单击事件检索时,为什么我的输入值等于 ''?

[英]Why is my input value equal '' when I retrieved with a button click event?

Why does the activityInput.value always equal an empty string.为什么activityInput.value 总是等于一个空字符串。 This seems very simple, I feel stupis asking something so simple but I cant get it to work.这看起来很简单,我觉得问这么简单的事情很愚蠢,但我无法让它发挥作用。 Please help me!请帮我!

 <div id = "activityListContainer">
      
          <input type ="text"  class = "input" value = '' id = "activityListInput"  name = "activityListInput" >
          <label for = 'activityListInput'></label>
          <button  class = "button" id = "activitySubmit">
              submit activity
          </button>
    
        <div id = 'activityList' ></div>
</div>

function addActivityListEntry(){
    
  let activityListContainer = document.getElementById('activityList');
       let newActivity = document.querySelector("#activityListInput").value
       console.log(newActivity)
 };

let activitySubmitButton = document.getElementById('activitySubmit');
activitySubmitButton.addEventListener('click', e => {
  e.preventDefault();
  addActivityListEntry()
 })

I can't seem to find where there should be an我似乎找不到应该有的地方

activityInput.value

all you have is an你所拥有的只是一个

activityListInput.value

This html file should work and shows the logs for "activityListInput"这个 html 文件应该可以工作并显示“activityListInput”的日志

 <!DOCTYPE html>
<head></head>
<body>
    
    <div id = "activityListContainer">
        
    <input type ="text"  class = "input" value = '' id = "activityListInput"  name = "activityListInput" >
    <label for = 'activityListInput'></label>
    <button  class = "button" id = "activitySubmit">
        submit activity
    </button>
    
    <div id = 'activityList' ></div>
</div>

</html>
<script>
    function addActivityListEntry(){
    
    let activityListContainer = document.getElementById('activityList');
         let newActivity = document.querySelector("#activityListInput").value
         console.log(newActivity)
   };
  
  let activitySubmitButton = document.getElementById('activitySubmit');
  activitySubmitButton.addEventListener('click', e => {
    e.preventDefault();
    addActivityListEntry()
   })
</script>
</body> 

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

相关问题 为什么输入值不会更新? 当我点击发送按钮时? - Why the input value wont update ? when i click the send button? 当我点击按钮编辑我的值时,状态不会更新输入值 - When I click on button to edit my value, the state don't update the input value 如何从按钮单击事件中获取输入值? - How can I get the value of an input from a button click event? <button>当我按下 Enter 时,</button>为什么会触发“click”事件<button>?</button> - Why does a `click` event get triggered on my <button> when I press Enter on it? 为什么当我的页面加载而不是单击我的单选按钮时触发我的 onchange 事件? - Why does is my onchange event triggered when my page loads instead of when I click my radio button? 为什么我第一次点击+按钮没有增加输入值? - Why does my first click on the + button not increase the value of the input? 为什么当我单击添加另一个图像按钮时,我之前的输入字段的值会消失? - Why are the values of my previous input field disappearing when I click on the ADD ANOTHER IMAGE button? 当我点击一个按钮时,我的输入标签变为空 - My input tag becomes empty when I click on a button 单击按钮时输入框的变化值 - Cant change value of input box when I click a button 单击按钮时如何获得输入值 - How I get input value when button is click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM