[英]JQuery set custom attribute from value
假设此代码:
<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'));
});
并正常工作。
现在我还有另一个组件
<select id="myselect-name" my_attribute = /* here I want to read Value*/ class="myclass" ...
在这种情况下, my_attribute
必须读取$(this).val()
。
有没有一种方法可以将value
属性设置为自定义属性?
谢谢。
无论是哪种方式(内联或单独),您都需要根据下拉的change事件来设置/更改custom_attribute的值,如下所示:
排队:
<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>
分离:
<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.