【问题标题】:Add confirm prompt before saving x-editiable在保存 x-editable 之前添加确认提示
【发布时间】:2015-04-28 12:45:05
【问题描述】:

如何在保存 x-editable (http://vitalets.github.io/x-editable/) 之前添加确认提示?如果对确认提示的响应是否定的,x-editable 对话框应该关闭并且值不应该更新。

我想出了这个Fiddle,但第一个解决方案有点冗长,第二个解决方案(基于x-editable prompt before updating the value)不起作用。

Test 1<a href="javascript:void(0)" id="name1"></a>
<br>
Test 2<a href="javascript:void(0)" id="name2"></a>

$('#name1').on('shown', function(e, editable) {
    var button=$('input.myClass').parent().next().find('button.editable-submit');
    console.log(button,e,editable,this);
    button.click(function(){
        if(!confirm("Are you sure you wish to proceed?")){
            editable.hide();
            return false;
        }
    });
});

$('#name1').editable({inputclass:'myClass'});

$('#name2').editable({
    validate: function (x) {
        console.log(x,this);
        if(!confirm("Are you sure you wish to proceed?")){
            //Doesn't work
            return false;
        }
    }
});

【问题讨论】:

    标签: javascript jquery x-editable


    【解决方案1】:

    x-editable documentation 表示当输入无效时需要返回一个字符串。我已经用一个工作样本更新了你的fiddle

    $('#name2').editable({
        validate: function (x) {
            console.log(x,this);
            if(!confirm("Are you sure you wish to proceed?")){
                // Hide the editable
                $('#name2').editable('toggle');
                return "invalid";
            }
        }
    });
    

    【讨论】:

    • 我希望结果与我的第一个解决方案相同。您的解决方案肯定更简洁,但会显示错误文本并且不会关闭对话框。我们可以在validate 回调中获取对话框对象以便隐藏它吗?
    • @user1032531 我已经通过切换调用更新了示例。小提琴也更新了。
    • 我刚刚想出了几乎相同的东西,但使用了$(this).editable('hide');。按照您的建议返回字符串是关键!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多