Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
当用户单击时,如何从jQueryUI对话框中获取按钮id的值?_Jquery_User Interface - Fatal编程技术网

当用户单击时,如何从jQueryUI对话框中获取按钮id的值?

当用户单击时,如何从jQueryUI对话框中获取按钮id的值?,jquery,user-interface,Jquery,User Interface,我想动态创建一个JQuery UI对话框。对话框中按钮的数量取决于我的json数据。我需要的按钮id时,用户点击它。我的代码是: var deviceInfo = { "devices": [ { "deviceID": "11", "devicename": "test12" }, { "deviceID": "12", "devicename

我想动态创建一个JQuery UI对话框。对话框中按钮的数量取决于我的json数据。我需要的按钮id时,用户点击它。我的代码是:

var deviceInfo = {
    "devices": [
        {
            "deviceID": "11",
            "devicename": "test12"
        },
        {
            "deviceID": "12",
            "devicename": "test12"
        }
    ]
};

for (var x in deviceInfo["devices"]) {

    arrButtons.push({
        text: "DeviceID:" + deviceInfo["devices"][x]["deviceID"], id: deviceInfo["devices"][x]["deviceID"], click: function () {
            currentDeviceID = deviceInfo["devices"][x]["deviceID"];

            console.log("selected id:", currentDeviceID);

            $(this).dialog("close");
        }
    });

}

showDeviceID = function (dID) {
    console.log("deviceID", dID);
}

$("#dialog").dialog({
    modal: true,
    dialogClass: 'no-close',
    show: {
        effect: "blind",
        duration: 1000
    },
    hide: {
        effect: "explode",
        duration: 1000
    },
    buttons: arrButtons
});
每次我拿到最后一个身份证。你能帮我换一下吗

for (var x in deviceInfo["devices"]) {

    arrButtons.push({
        text: "DeviceID:" + deviceInfo["devices"][x]["deviceID"], id: deviceInfo["devices"][x]["deviceID"], click: function () {
            currentDeviceID = deviceInfo["devices"][x]["deviceID"];

            console.log("selected id:", currentDeviceID);

            $(this).dialog("close");
        }
    });

}
用于:


只需使用
click
处理程序的
event
属性即可

$('button').click(function(e)
{
   e.target.id; // gives you the id of the button clicked.
}

非常感谢你的评论。实际上我需要用户点击哪个按钮。每个按钮都有id,我把它和我的设备id放在一起,因为我需要用户选择显示哪个设备。谢谢@Leo的回复。我使用了你的更新版本,但是它显示了一个错误,比如:“uncaughttypeerror:cannotreadproperty'apply'of undefined”,非常感谢@dinmyte为你提供的巨大帮助。现在它工作了,我得到了用户点击的按钮id。再次感谢。
$('button').click(function(e)
{
   e.target.id; // gives you the id of the button clicked.
}