专注于对象JQuery

时间:2016-11-08 01:33:42

标签: jquery sweetalert

我正在使用Sweet Alert插件来显示提醒,如果我点击确定,我想要专注于我的文本框。

我的问题是: 1.如果我使用来自甜蜜警报的timer属性,则jquery中的焦点函数不起作用 2.但如果我没有使用来自甜蜜警报的属性,则jquery中的焦点功能正常工作。

也许有人可以帮我解决我的问题:D

我的剧本:

swal({
  title: "Auto close alert!",
  text: "I will close in 2 seconds.",
  timer: 2000
});
$('.classMyTextField').focus();

ps:我试图在swal函数之间使用2个焦点函数,但仍无效:D

由于

3 个答案:

答案 0 :(得分:0)

你可以尝试这段代码吗?

swal({
  title: "Auto close alert!",
  text: "I will close in 2 seconds.",
  timer: 2000
}, function(){
  swal.close();
  $('.classMyTextField').focus();
});

答案 1 :(得分:0)

只需添加一个回调函数作为swal()

的第二个参数
swal({
    title: "Auto close alert!",
    text:  "I will close in 2 seconds.",
    timer: 2000
  },
  function(){
    // SweetAlert confirmed, do something

    $('.classMyTextField').focus();
  }
);

当SweetAlert 确认取消时执行某些操作:

swal({
    title: "Auto close alert!",
    text:  "I will close in 2 seconds.",
    timer: 2000
  },
  function(isConfirmed){
    if(isConfirmed){
       swal("Confirmed!", "You confirmed the alert.", "success");
       $('.classMyTextField').focus();
    }
    else{
       swal("Cancelled!", "You cancelled the alert.", "warning");
    }           
  }
);

<强>更新

仅显示没有确认或取消按钮的通知:

swal({
    title: "Auto close alert!",
    text:  "I will close in 2 seconds.",
    timer: 2000,
    showConfirmButton: false,
    showCancelButton: false
  },
  function(){
    // alert closed
    $('.classMyTextField').focus();
  }
);

答案 2 :(得分:0)

执行此操作的最佳有效方法是在使用“;”关闭swal之前使用.then(function(){

使用您的代码,就像那样:

swal({
  title: "Auto close alert!",
  text: "I will close in 2 seconds.",
  timer: 2000
}).then(function(){
  $('.classMyTextField').focus();
});