Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中获取动态生成元素的属性值(通过ajax)_Javascript_Html - Fatal编程技术网

在javascript中获取动态生成元素的属性值(通过ajax)

在javascript中获取动态生成元素的属性值(通过ajax),javascript,html,Javascript,Html,我通过ajax将值生成到表中。该值为s。 在这个角色中有一个锚定,它将位于所有这些角色中。这个锚定标记有一个类,我用javascript引用它 这个角色中的锚定标记有一个引用javascript方法的onclick函数,我试图使用这个方法来获取单击的锚定标记的一个属性。 但不幸的是,当我单击角色中的锚标记时,我得到了未定义的错误 <tr> <td> <a href="#" onClick="JavaScri

我通过ajax将值生成到表中。该值为s。 在这个角色中有一个锚定,它将位于所有这些角色中。这个锚定标记有一个类,我用javascript引用它

这个角色中的锚定标记有一个引用javascript方法的onclick函数,我试图使用这个方法来获取单击的锚定标记的一个属性。 但不幸的是,当我单击角色中的锚标记时,我得到了未定义的错误

    <tr>

 <td>
                        <a href="#" onClick="JavaScript:filltransfercombo();" class="btn btn-default transferchat" data-operatorid="@avisitor.OperatorID" data-toggle="modal" data-target="#transferModal" title="Transfer chat">
                            <img src="~/Content/images/chaticons/transfer.png" height="20" width="20" />
                        </a>
                    </td>

</tr>

我的意图是收集这个属性——数据运算符ID。我想在javascript中使用它进行一些操作

下面是我的javascript

<script type="text/javascript">

 function filltransfercombo() {


            var result = "";
            var active = $('a').index(this);
            var currentoperatorid = $(active).attr("data-operatorid"); //This is the id of the currently attending operator

            console.log(currentoperatorid);

        }

</script>

函数filltransfercombo(){
var结果=”;
var active=$('a')。索引(this);
var currentoperatorid=$(活动).attr(“数据运算符id”);//这是当前参与的运算符的id
控制台日志(currentoperatorid);
}

onclick
事件不需要
javascript:
符号。。。如果您将方法设置为
href
,则可以将其放在该位置

另外,从
onclick
事件,您可以访问
变量,而不是从方法。但是,您可以将其作为参数传递,因此:

<a href="#" onClick="filltransfercombo(this);" class="btn btn-default transferchat" data-operatorid="@avisitor.OperatorID" data-toggle="modal" data-target="#transferModal" title="Transfer chat">

这可以通过jQuery轻松完成

删除锚点上的“onClick”属性,而是将单击功能绑定到锚点标记(仅在表中)。然后在该函数中添加处理代码

这样做:

<script type="text/javascript">
$(document).ready(function() {
    $('table tr td a').click(function() {
        var result = "";
        var currentoperatorid = $(this).data("operatorid"); //This is the id of the currently attending operator

        console.log(currentoperatorid);
    });
});
</script>

$(文档).ready(函数(){
$('table tr td a')。单击(函数(){
var结果=”;
var currentoperatorid=$(this).data(“operatorid”);//这是当前参与的运算符的id
控制台日志(currentoperatorid);
});
});

当然,您需要确保jQuery javascript库也包含在此页面中。

我认为您混淆了jQuery和ajax。您是什么意思@SimpleJI试图在jQuery中使用on()方法来获取当前单击元素的属性。但在方法上并没有触发。所以我觉得我应该使用javascriptI,我的意思是,看起来您使用的是静态html,我没有看到您的代码中有任何ajax请求。您刚刚为我节省了一个小时的编码时间。非常感谢。它就像魔术一样,一切都进行得很顺利。你是婴儿潮兄弟。我很感激@lcsalazar如果属性是一个
data..
标记,那么使用
$(sender.data(“operatorid”)不是更有效/更正确一点吗?@熵,这是一个有趣的点。。。有很多关于这两种表现的讨论。。。请看@LcSalazar,这是一个很棒的网站-不知道它存在。真的很有趣。。。
<script type="text/javascript">
$(document).ready(function() {
    $('table tr td a').click(function() {
        var result = "";
        var currentoperatorid = $(this).data("operatorid"); //This is the id of the currently attending operator

        console.log(currentoperatorid);
    });
});
</script>