获取javascript中动态生成的元素(通过ajax)的属性值
getting values of attributes of dynamically generated elements (through ajax) in javascript
我正在通过ajax将值生成到一个表中。这些值为s。在这个角色中有一个锚,它将在所有这些角色中。这个锚标记有一个类,我用它来用javascript引用它。
这个角色中的anchor标签有一个onclick函数引用了一个javascript方法,我正试图使用这个方法来获取点击的锚标签的一个属性。但不幸的是,当我点击角色中的锚标签时,我得到了未定义的错误。
<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>
我的意图是收集这个属性——数据运算符。我想在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>
onclick
事件不需要javascript:
表示法。。。你可以把它放在href
上设置方法的情况下。
此外,从onclick
事件中,您可以访问this
变量,而不是从方法中访问。然而,您可以将其作为参数传递,因此:
<a href="#" onClick="filltransfercombo(this);" class="btn btn-default transferchat" data-operatorid="@avisitor.OperatorID" data-toggle="modal" data-target="#transferModal" title="Transfer chat">
和js:
function filltransfercombo(sender) {
var result = "";
var currentoperatorid = $(sender).attr("data-operatorid"); //This is the id of the currently attending operator
console.log(currentoperatorid);
}
这可以通过jQuery轻松完成。
删除锚点上的"onClick"属性,而是将click函数绑定到锚点标记(仅在表中)。然后在该函数中添加处理代码。
这样做:
<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>
当然,您需要确保jQuery javascript库也包含在此页面中。
相关文章:
- Javascript/Ajax:通过点击按钮检查有效的电子邮件和电话号码
- Ajax通过单击javascript按钮更改php状态
- AJAX 通过 jQuery 调用
- AJAX 通过函数更新数据库
- 使用 Jquery/AJAX 通过 JSP 页面中的 Linux 绝对路径显示文本文件内容
- Drupal 7 AJAX 通过hook_menu调用
- 使用 jQuery AJAX 通过 PHP 将表单数组传递给 Javascript
- Ajax 通过 jquery 加载,内容未显示
- 如何通过ajax通过SSL从本地html文件调用web api
- 使用ajax通过邮件发送信息时出现问题
- 使用AJAX通过HTML选择触发的JavaScript刷新网页
- 使用ajax通过POST向php传递多个参数
- Jquery ajax通过PHP文件将表单数据插入数据库前需要第二次点击
- Ajax通过javascript路由,控制器应该返回json并返回html页面
- AJAX通过jQuery更新页面
- Ajax——通过onload和onunload启动和结束会话
- Coldfusion使用ajax通过下拉选择填充表单
- 使用jQuery AJAX通过FormData上传文件
- 关于Ajax通过纯javascript上传图像
- Ajax通过post将文本从一个文本区域复制到另一个文本区域