javascript - 从数据属性获取输入值

标签 javascript html input onchange

我在下面有这段代码,用户可以在其中选择 - 每个下拉选项都有不同的数据期限:

<div class="ten columns">
  <p class="slider-label">Loan Tenure (Years)</p>
  <!-- <input type="text" class="loan-tenure numeric-only" /> -->
  <div class="field">
    <div class="picker picker-dd2">
      <select class="dropdown2 loan-tenure">
        <option class="dropdown2-opt" data-tenure=“0.0298” value="1">1</option>
        <option class="dropdown2-opt" data-tenure=“0.0387” value="2">2</option>
        <option class="dropdown2-opt" data-tenure=“0.0418” value="3">3</option>
        <option class="dropdown2-opt" data-tenure=“0.0434” value="4">4</option>
        <option class="dropdown2-opt" data-tenure=“0.0444” value="5">5</option>
      </select>
    </div>
  </div>
</div>

我想要的是将输入值设置为上面的数据保留 - :

                <input type="hidden" class="loan-rate" value="??" />

我需要将 onchange() 函数添加到上面的下拉列表中吗?

最佳答案

编辑、更新

I meant - getting the data-tenure and this will be the input value. So if the user selects 1, the input value will be 0.0298. If the user selects 2 the input value will be 0.0387

您可以在 .dropdown2 元素、document.querySelector() 和选择器 "input.loan-rate 中使用 change 事件",在 change 事件处理程序中使用 event.target Element.dataset 设置 .value 属性

document.querySelector(".dropdown2").addEventListener("change", function(event) {
  // set value here   
  document.querySelector("input.loan-rate")
  .value = event.target.dataset.tenure; 
})

关于javascript - 从数据属性获取输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38495347/

相关文章:

html - 100% 的子 div 高度被忽略

javascript - 从外部javascript文件调用html中的函数

html - 使用图像标签和 CSS 显示本地 HTML 文件的 C# 控件/代码

c - 为什么 getchar() 不停止读取 C 中的字符串?

ios - UITextField 键击和复制/粘贴之间的区别

javascript - 修改对象的 for...of 循环行为

javascript - 如何在 jQuery Mobile 中替换按钮文本

javascript date.parse在chrome和其他浏览器中的区别

javascript - 如何从 HTML 元素内部获取值并在 JS 中将其设置为变量

javascript - 自动切换到表格单元格中的下一个输入字段