简体   繁体   中英

jquery.confirm in .click() function

I'm using a nice library named jquery.confirm and I have problem when I want to use its .confirm() method in the jQuery .click() function.

Here is my code:

$('#car-url').click(function()
{
    var empty = true;
    $(".departurePlace input, .arrivalPlace input, .cost input").each(function()
    {
        var input = $(this);
        if (input.val() != '' || input.val() != 0)
            empty = false;
    });
    if (empty == false)
        $(".confirm").confirm();
});

As you can see I just want to check the inputs before eventually show the dialog box.

I first contacted the developer because I thought that was a bug, but according to him it is not.

Any ideas?

Edit: It can not be my selectors because when I try this:

$('#car-url').click(function()
{
    var empty;
    $(".departurePlace input, .arrivalPlace input, .cost input").each(function(){
    var input = $(this);
    if (input.val() != '' || input.val() != 0)
    {
        alert('test');
    }
});

I have the alert box as desired when one input is not empty.

I don't think your issue has anything to do with the confirm plugin. As Raibaz suggested , it's your selectors.

Change your ids from my input1 to my-input1 .

A couple fiddles to illustrate:

Fiddle: selectors with spaces (based on your original code)

Fiddle: selectors without spaces

$('#my-url').click(function() {
    var empty = true;

    $("#my-input1, #my-input2").each(function() {
        if ($(this).val()) {
            empty = false;
            return false;
        }
    });

    if (! empty) {
        confirm('confirmed');
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM