简体   繁体   English

访问html元素值javascript

[英]access html element value javascript

I've got three dropdown menu's which are dynamically filled from the database each time I select an option in the previous dropdown menu. 我有三个下拉菜单,每次我在上一个下拉菜单中选择一个选项时,它们都会从数据库中动态填充。

Now I want to access the values in these dropdown menu's, so that I can build a SQL-query somewhere later. 现在,我想访问这些下拉菜单中的值,以便稍后可以在某个地方构建SQL查询。

I've used the following code to access the HTML elements: 我使用以下代码访问HTML元素:

$( window ).load(function(){
    var e = document.getElementById("slctTable");
    var slctTableValue = e.value;
    console.log(slctTableValue);
});

This code only works the first time the page loads, so when I mess around with the dropdown menu's, nothing changes. 该代码仅在页面首次加载时起作用,因此当我在下拉菜单上乱七八糟时,没有任何变化。

What I want now, is that each time I select a value in the dropdown menu, it updates the slctTableValue variable. 我现在想要的是,每次我在下拉菜单中选择一个值时,它都会更新slctTableValue变量。

Bind change event to the drop-down with jquery, then this event will fire whenever there is a change happened on the selected value. 将更改事件绑定到带有jquery的下拉菜单,然后只要所选值发生更改,就会触发此事件。

$("#slctTable").change(function() {
  var slctTableValue = $(this).val();
  console.log(slctTableValue);
});

You can detect change event like this and update value: 您可以像这样检测更改事件并更新值:

    document.getElementById('slctTable').addEventListener('change',function(){
       var e = document.getElementById("slctTable");
       var slctTableValue = e.value;
       console.log(slctTableValue);
    });

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

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