JQuery按钮数据返回为Null

JQuery Button Data Returning As Null?

本文关键字:Null 返回 数据 按钮 JQuery      更新时间:2023-09-26

我有一个按钮,当我点击它时,我希望html对象(又名按钮)作为参数传递给另一个javascript函数。我希望javascript函数从按钮中的元素打印数据hi。

HTML按钮

<button type = "button" onclick = "whoIsRdns(this)" class="dns-information btn btn-xs btn-info pull-right" data-toggle="modal" data-target = "#whois_rdns_modal" data-path="{{ path( '_who_is_rdns', { 'peer': peer.number, 'ip': peer.mac } ) }}" data-hi = "hi2">
<i class="icon-search"></i>
</button>

JS函数(W/JQUERY)

    function whoIsRdns(thisButton){
    //Enable jQuery properties from the param of the HTML object
        var btn = $(thisButton);
        var test = btn.data('hi');
        console.log('Value is ' + test);
}

为什么测试会返回null?

var btn = $("thisButton");不应该是var btn = $(thisButton);(没有引号)

只是一个打字错误

$("thisButton") !== $(thisButton);

去掉引号,这样您就不会寻找标记名为thisButton 的元素

var btn = $("thisButton");

需要

var btn = $(thisButton);