简体   繁体   English

如何使用 Javascript 从父 div 获取输入并从中获取实时值

[英]How to take the inputs from a parent div and take realtime values from them with Javascript

I encountered a problem that I don't really know how to approach.我遇到了一个我真的不知道如何解决的问题。 For example, I will have a div to which I will apply a data attribute例如,我将有一个 div 应用数据属性

<div class="filter-content" data-filters>
    <div class="filter-attributes">
      <input type="checkbox" name="S1P3">
      <input type="checkbox" name="S1P4">
      <input type="checkbox" name="S1P5">
      <input type="checkbox" name="S1P6">
      <input type="checkbox" name="S1P7">
      <input type="checkbox" name="S1P8">
      <input type="checkbox" name="S1P9">
      <input type="checkbox" name="S1P10">
      <input type="checkbox" name="S1P2">
      <input type="checkbox" name="S1P12">
      <input type="checkbox" name="SP52">
      <input type="checkbox" name="CI">
      <input type="radio" name="CI2">
      <input type="radio" name="CI3">
      <select name="test">
        <option value="test">Test</option>
        <option value="test1">Test1</option>
        <option value="test2">Test2</option>
        <option value="test3">Test3</option>
        <option value="test4">Test4</option>
        <option value="test5">Test5</option>
        <option value="test6">Test6</option>
        <option value="test7">Test7</option>
      </select>
    </div>
  </div>
/**
     * @return {void}
     */
    getFiltersData: function() {
      let filtersInputs = document.querySelector('[data-filters]');
      console.log(filtersInputs)
      
    },

But I don't know how I could add an event on each input / select there and take the value when it changes Is there a simple idea?但我不知道如何在每个输入/ select 上添加一个事件,并在它改变时取值有一个简单的想法吗?

I guess you need something like this:我想你需要这样的东西:

let filtersInputs = document
  .querySelector('[data-filters]')
  .querySelectorAll('input, select');

filtersInputs.forEach((input)=>{
  input.addEventListener('change', (e)=>{
    console.log(e.target.value);
  })
})

Note that you're not assigning any values in your HTML.请注意,您没有在 HTML 中分配任何值。

暂无
暂无

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

相关问题 如何从 javascript 中的控制台获取多个输入? - How to take multiple inputs from console in javascript? 如何从输入中获取多个字符串并将它们放入同一数组中? - How to take multiple strings from inputs and place them into the same array? 从输入文本区域获取值并将它们放在一个<div> - Take values from input text area and place them in a <div> 尝试从两个不同的文本输入中获取值并将它们放在同一个列表项中 - Trying to take Values from two different text inputs and put them in the same list item 如何从网站上的用户那里获取某些输入并将这些值作为特定参数传递到我的 javascript function 中? - How can I take certain inputs from a user on a website and pass those values as specific parameters in my javascript function? 如何从 <form> 并分别显示输入值 <div> - How to take data from a <form> and display the input values in a separate <div> 如何从文本字段中获取用户输入,并在Google脚本中将其设置为变量? - How do I take user inputs from a textfield and make them variables in a Google Script? 如何从同一个输入框中获取多个输入并存储它们? - How Do I Take Multiple Inputs From the same Input Box and store them? 如何从文本框中获取内容并将其放在javascript中的变量和div中 - How to take content from a textbox and place it in both a variable and div in javascript 如何使用javascript从div获取打印的选定内容? - How to take print selected content from div using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM