我试图将属性及其值从 onclick 事件传递给 javascript 函数,但返回时未定义。
<script type="text/javascript">
function addParameters(control) {
alert(control._userid);
alert(control._flag);
}
<button text="Button" ID="btn1" _userid="datatest" _flag="datafromdatabaase" title="Add this to the list" onclick="addParameters(this);"> Button</button>
最佳答案
您不能像那样直接访问那些自定义属性。您需要通过元素的 attributes
属性访问它们。试试这个:
<button text="Button" ID="btn1" _userid="datatest" _flag="datafromdatabaase" title="Add this to the list" onclick="addParameters(this);"> Button</button>
<script type="text/javascript">
function addParameters(control) {
alert(control.attributes._userid);
alert(control.attributes._flag);
}
</script>
如果你想获取这些属性的值,试试这个:
<button text="Button" ID="btn1" _userid="datatest" _flag="datafromdatabaase" title="Add this to the list" onclick="addParameters(this);"> Button</button>
<script type="text/javascript">
function addParameters(control) {
alert(control.attributes._userid.nodeValue);
alert(control.attributes._flag.nodeValue);
}
</script>
关于javascript - 将 onclick 控件发送到 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33049450/