简体   繁体   English

如何使用其类从 div 标签获取表单数据?

[英]How can I get form data from a div tag by using its class?

I have code something like:我有类似的代码:

 var iconContainer = document.getElementById('iconContainer'); var icon = iconContainer.getElementsByClassName("item"); for (var i = 0; i < icon.length; i++) { icon[i].addEventListener("click", function() { var current = document.getElementsByClassName("active"); current[0].className = current[0].className.replace(" active", ""); this.className += " active"; }); }
 <form> <header> <div class="icon-line" id="iconContainer"> <div class="item active" id=""> <div class="group-icon"> <i class="fas fa-hdd"></i> </div> <span>SSD</span> </div> <div class="item" id=""> <div class="group-icon"> <i class="fas fa-server"></i> </div> <span>EKRAN KARTI</span> </div> <div class="item" id=""> <div class="group-icon"> <i class="fas fa-microchip"></i> </div> <span>İŞLEMCİ</span> </div> <div class="item" id=""> <div class="group-icon"> <i class="fas fa-memory"></i> </div> <span>RAM</span> </div> </div> </header> </form>

Also I have a selection.js that changes selected item's class from class="item" to class="item-active"另外我有一个 selection.js 将所选项目的类从 class="item" 更改为 class="item-active"

How can controll the selected item by using it's data?如何通过使用它的数据来控制所选项目?

I want to search in my data by using selected item.我想使用所选项目在我的数据中进行搜索。 For exapmle, you selected SSD, so that, I will search on my SSD.json file or you selected RAM, I will search on my ram.js例如,您选择了 SSD,因此,我将在我的 SSD.json 文件中进行搜索,或者您选择了 RAM,我将在我的 ram.js 中进行搜索

How can do this?怎么能做到这一点?

Note: I'm using Node.js on server side.注意:我在服务器端使用 Node.js。

Ok, firstly you have a for loop that change item's class to "active" one, we will continue adding code there.好的,首先你有一个for循环,将项目的类更改为“活动”类,我们将继续在那里添加代码。

var iconContainer = document.getElementById('iconContainer');
var icon = iconContainer.getElementsByClassName("item");

for (var i = 0; i < icon.length; i++) {
  icon[i].addEventListener("click", function() {
    var current = document.getElementsByClassName("active");
    current[0].className = current[0].className.replace(" active", "");
    this.className += " active";
    // ADD CODE HERE

  });
}

You'll have to do 2 things: get the selected item's value and send it to server by making a searh request with the value as params.您必须做两件事:获取所选项目的值并将其发送到服务器,方法是使用该值作为参数进行搜索请求。

Get the selected item's value by getting the 's innerText:通过获取 的innerText 来获取所选项目的值:

...
// ADD CODE HERE
var value = this.getElementsByTagName("span")[0].innerText;

And make a get request with the value with fetch API (or any libs that support make xhr request):并使用fetch API(或任何支持 make xhr 请求的库)发出带有值的 get 请求:

...
// ADD CODE HERE
var value = this.getElementsByTagName("span")[0].innerText;
fetch(`URL_TO_SERVER/?params=${value}`)
   .then(res => {
        // do what you want with search result
   }

The rest is your server's work to handle the request, take params, search on respective file and return the search result to client.剩下的就是您的服务器处理请求、获取参数、搜索相应文件并将搜索结果返回给客户端的工作。

That's all.就这样。

In better way, instead of using value from innerText as param, you should use data-attribute for better request.更好的方法是,不要使用来自innerText值作为参数,您应该使用data-attribute来获得更好的请求。 You can read here: https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes你可以在这里阅读: https : //developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

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

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