如何通过javascript更新html自定义按钮输入中的子属性

时间:2017-05-27 14:36:43

标签: javascript getelementbyid

我想在点击更新按钮

之前更新radiobutton点击的数据百分比值

HTML:

<input type="radio" name="percentage" value="50" onclick="changeVal(this.value)"> Happy 50 percent</input>

</br>

<input type="radio" name="percentage" value="75" onclick="changeVal(this.value)"> Happy 75 percent</input>
</br>

<button class="btn btn-danger my-btn" id = "myUpdateBtn" data-id="1" data-name="John" data-summary="John is Happy" data-percent="50" >Click to Update</button>

javascript:我的脚本功能不更新按钮子属性值。请帮忙。

changeVal(val){
 alert(val);
 document.getElementById("myUpdateBtn").value('data-price') = val;
}

1 个答案:

答案 0 :(得分:1)

要访问data属性,您必须这样做:

&#13;
&#13;
var val = document.getElementById('21').dataset.id;
var msg = document.getElementById('21').dataset.name;

alert(val); //Alert '21'
alert(msg); //Alert 'HiDiv'

document.getElementById('21').dataset.lastName = 'Rod'; //Create new data attribute

console.log(document.getElementById('21')); //View changes
&#13;
<div data-id='21' id='21' data-name = 'HiDiv'>

</div>
&#13;
&#13;
&#13;