Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.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 jQuery-访问按钮单击事件中的数据属性_Javascript_Jquery_Asp.net Mvc - Fatal编程技术网

Javascript jQuery-访问按钮单击事件中的数据属性

Javascript jQuery-访问按钮单击事件中的数据属性,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我有以下按钮: <button id="btnDeleteCoverage" type="button" class="btn btn-info btn-xs delete-btn" data-toggle="modal" data-target="#deleteCoverageModal" data-analystId="@item.AnalystID" data-secSymbol="@item.SecSymbol" data-Status="@item.Status"><

我有以下按钮:

<button id="btnDeleteCoverage" type="button" class="btn btn-info btn-xs delete-btn" data-toggle="modal" data-target="#deleteCoverageModal" data-analystId="@item.AnalystID" data-secSymbol="@item.SecSymbol" data-Status="@item.Status"><span class="glyphicon glyphicon-trash"></span></button>
我想访问在按钮的以下单击事件中设置的值:

 $("#btn_delete_coverage").click(function (e) {
            // get the value for the data attributes here and use in postdata
            var postdata = { "analystID": /* use value here */, "symbol": /* use value here */, "status": /* use value here */ };

     //etc
});
编辑:

我尝试过使用jQuery数据和attr函数,但无法使其工作

我试过了

     var analystID = $(this).attr('data-analystId');

及 var-analystID=$(this.data('data-analystID')

但是每次都要获取“未定义”。

使用.attr()方法访问自定义属性。 例如:

 $("#btn_delete_coverage").click(function (e) {
        // get the value for the data attributes here and use in postdata
        var analystID = $(this).attr('data-analystId');
        var postdata = { "analystID": analystID, "symbol": /* use value here */, "status": /* use value here */ };

 //etc
});
编辑:
显然,选择器是个问题,它应该是$(“#btnDeleteCoverage”)

我在Chrome的开发工具中插入了html,并使用了:$(“#btnDeleteCoverage”).attr('data-analystid');它返回了“@item.AnalystID”,所以我想说使用attr是有效的“我已经尝试使用jQuery数据和attr函数,但无法使其工作”——在您的示例中,我没有看到
.data()
.attr()
,也没有看到错误消息。请参阅我的编辑。
.attr()
版本应该可以工作,
.data()
版本不可以(请参阅文档了解原因)。但是每次都得到“未定义”->请添加一个显示实际问题的选项。感谢您的帮助,Andreas。这是我的一个错误,如下所述。尝试了它,但得到了“未定义”。请看我的编辑。我还看到它应该是$(“btnDeleteCoverage”)而不是$(“#btn_delete_coverage”)。是的,我刚刚回来注意到这一点。但谢谢你指出这一点。这就是问题所在。首选方法是
var-analystID=$(This.data('analystID')-但根据规范,属性必须全部为小写
     var analystID = $(this).data('data-analystId');
 $("#btn_delete_coverage").click(function (e) {
        // get the value for the data attributes here and use in postdata
        var analystID = $(this).attr('data-analystId');
        var postdata = { "analystID": analystID, "symbol": /* use value here */, "status": /* use value here */ };

 //etc
});