简体   繁体   English

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

[英]how to update sub attributes in html custom button input via javascript

I want to update data-percent value on a radiobutton click before clicking the update button 我想在单击更新按钮之前更新单选按钮单击的数据百分比值

html: 的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: my script function does not update button sub attribute value. javascript:我的脚本功能无法更新按钮子属性值。 please help. 请帮忙。

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

In order to access data attributes you have to do it like this: 为了访问data属性,您必须这样做:

 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 
 <div data-id='21' id='21' data-name = 'HiDiv'> </div> 

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

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