[英]JQuery set custom attribute from value
Suppose this code: 假设此代码:
<input id="myinpunt-name" my_attribute="1" class="myclass" ...
<checkbox id="myinpunt-name" my_attribute="2" class="myclass" ...
$('.myclass').change(function() {
dosomething($(this).attr('my_attribute'));
});
and works correctly. 并正常工作。
Now I have also another component 现在我还有另一个组件
<select id="myselect-name" my_attribute = /* here I want to read Value*/ class="myclass" ...
In this case my_attribute
has to read $(this).val()
. 在这种情况下,
my_attribute
必须读取$(this).val()
。
In there a way to set value
attribute to a custom attribute ? 有没有一种方法可以将
value
属性设置为自定义属性?
thanks. 谢谢。
On either way (inline or separate), you will require to set / change custom_attribute's value based on the change event of drop-down as following: 无论是哪种方式(内联或单独),您都需要根据下拉的change事件来设置/更改custom_attribute的值,如下所示:
Inline: 排队:
<select id="myselect-name" my_attribute="1"
onchange="javascript: this.setAttribute('my_attribute', this.value);">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
Separate: 分离:
<select id="myselect-name" my_attribute="1" class="myclass">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
$('.myclass').change(function() {
$(this).attr('my_attribute', $(this).val());
alert($(this).attr('my_attribute'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.