Jquery UI - 对话框关闭,按下了哪个按钮

时间:2022-08-23 18:00:06

I am using a jquery ui dialog as a prompt. The "prompt" has to buttons, "Ok" and "Cancel". The problem here is, it extracts the input fields value in the dialog when .dialog("close") is triggered and my only validation is that the length of the input field has to be more than 0 chars. This means that even when you type something and press cancel the text from the prompt will be submittet. My thought was to find out what button was pressed... Anyone know a solution to this?

我使用jquery ui对话框作为提示。 “提示”必须是按钮,“确定”和“取消”。这里的问题是,当触发.dialog(“close”)时,它会在对话框中提取输入字段值,我唯一的验证是输入字段的长度必须大于0个字符。这意味着即使您键入内容并按取消,提示中的文本也将被提交。我的想法是找出按下了什么按钮......任何人都知道解决方案吗?

My current event code:

我目前的事件代码:

$("#addBusinessarea").click(function(){
    createPrompt("Add new business area", "Business area name:");
    $( "#prompt" ).bind( "dialogclose", function(event, ui) {
        if($("#promptValue").val().length > 0){
            // Add business area 
        }
    });
});

2 个解决方案

#1


1  

To properly solve change the way you've defined buttons for jQuery UI dialog. It can look like this (notice that you can have different click handlers for different buttons):

要正确解决更改为jQuery UI对话框定义按钮的方式。它看起来像这样(注意你可以为不同的按钮设置不同的点击处理程序):

$("#modal").dialog({
        buttons: {
            Yes: {
                text: 'Yeeees!',
                click: function() {
                    alert('I clicked yes!');
                }
            },
            No: {
                text: 'Hell no!',
                click: function() {
                    alert('I clicked no!');
                }
            }
        }
    })

#2


1  

Old question, but this question came up for me, and I found the right answer, with help from jQuery UI Dialog buttons

老问题,但是这个问题出现了,我在jQuery UI Dialog按钮的帮助下找到了正确的答案

event.target is your button.

event.target是你的按钮。

$( "#prompt" ).bind( "dialogclose", function(event, ui) {
    if ($(event.target).text() != "Cancel") {
        if($("#promptValue").val().length > 0){
            // Add business area 
        }
    }
});

#1


1  

To properly solve change the way you've defined buttons for jQuery UI dialog. It can look like this (notice that you can have different click handlers for different buttons):

要正确解决更改为jQuery UI对话框定义按钮的方式。它看起来像这样(注意你可以为不同的按钮设置不同的点击处理程序):

$("#modal").dialog({
        buttons: {
            Yes: {
                text: 'Yeeees!',
                click: function() {
                    alert('I clicked yes!');
                }
            },
            No: {
                text: 'Hell no!',
                click: function() {
                    alert('I clicked no!');
                }
            }
        }
    })

#2


1  

Old question, but this question came up for me, and I found the right answer, with help from jQuery UI Dialog buttons

老问题,但是这个问题出现了,我在jQuery UI Dialog按钮的帮助下找到了正确的答案

event.target is your button.

event.target是你的按钮。

$( "#prompt" ).bind( "dialogclose", function(event, ui) {
    if ($(event.target).text() != "Cancel") {
        if($("#promptValue").val().length > 0){
            // Add business area 
        }
    }
});