简体   繁体   English

“ data-”属性作为javascript参数在chrome中不起作用

[英]“data-” attribute as javascript parameter is not working in chrome

HTML: HTML:

<select name="employee_ID" id="employee_ID" onchange="$('#IncomeTaxDeduction').val($('#employee_ID').find(':selected').data('incomtax'));"> 
             <option value="" disabled selected style="display:none;">Select</option>          
          <?php foreach ($employee_data as $emp_opt_value): ?>
             <option  value="<?php echo $emp_opt_value['ID']; ?>" data-incomtax= "<?php echo $emp_opt_value['IncomeTaxPercent']; ?>" onclick="deductions(this)" > <?php echo $emp_opt_value['ID']; ?></option>
          <?php endforeach; ?>
</select>

JS: JS:

var itdeduction;

function deductions(obj){
   itdeduction = parseFloat(obj.getAttribute('data-incomtax'));

   alert(itdeduction);
}

This 'data-' attribute works very well in firefox but it is not working in google chrome. 这个'data-'属性在Firefox中效果很好,但在谷歌浏览器中不起作用。 Can somebody kindly help me. 有人可以帮我吗。

Following is the approach to get data attributes is JS: 以下是获取数据属性的方法是JS:

 <script> function getData() { alert(document.getElementById('sample').getAttribute('data-name')); } </script> <li id="sample" data-name="test">Sample</li> <br/> <button onClick="getData();">Get Attribute Value</button> 

Your code edit: 您的代码编辑:

 <script> var itdeduction; function deductions(){ var element = document.getElementById('employee_ID'); itdeduction = parseFloat(element.options[element.selectedIndex].getAttribute('data-incomtax')); alert(itdeduction); } </script> <select name="employee_ID" id="employee_ID" onchange="deductions();"> <option value="12" data-incomtax= "20">Some Name</option> <option value="13" data-incomtax= "30">Another Name</option> </select> 

What does element.options[element.selectedIndex] do? element.options [element.selectedIndex]有什么作用?

It selects the selected option for the select box. 它为选择框选择选定的选项。 example: if somename is selected then element.options[element.selectedIndex] will give us <option value="12" data-incomtax= "20">Some Name</option> 示例:如果选择了某个名称,那么element.options[element.selectedIndex]将为我们提供<option value="12" data-incomtax= "20">Some Name</option>

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

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