从页面后面的代码显示时,JQuery 对话框上的按钮焦点不起作用 - asp.net / c#

Posted

技术标签:

【中文标题】从页面后面的代码显示时,JQuery 对话框上的按钮焦点不起作用 - asp.net / c#【英文标题】:Button focus on JQuery Dialog doesn't work when showing from code behind the page - asp.net / c# 【发布时间】:2021-03-25 04:32:03 【问题描述】:

我从后面的代码中调用 confirmDialog 方法,该方法工作正常,但由于某些原因,焦点未设置在取消按钮上。当我点击弹出框外的模态灰色区域时设置焦点。

有没有办法在不点击页面的情况下将默认焦点设置在取消按钮上?

对话框打开后

鼠标点击灰色区域后;

这是代码;

 function confirmDialog() 

 $("#dialog-confirm").html("some message here");
 $("#dialog-confirm").dialog(
autoOpen: false,
resizable: false,
height: "auto",
width: "auto",
modal: true,
title: 'Warning!',
buttons: 
  "Ok": function() 
    $(this).dialog("close");
    return true;
  ,
  Cancel: function() 
    $(this).dialog("close");
    return false;
  
,
open: function() 
  $(this).siblings('.ui-dialog-buttonpane').find("button:contains('Cancel')").focus();

);

        return false;

【问题讨论】:

试试这样:$(".ui-dialog-buttonpane").find("button:eq(1)").focus(); 【参考方案1】:

您可以使用“nth-child(2)”搜索模态中的第二个按钮:

$(this).parent().find('button:nth-child(2)').focus();

DEMO

【讨论】:

不工作....我的代码甚至可以在小提琴中工作,但不能在浏览器上工作。 你用的是什么浏览器? Chrome 和 Edge 请再试一次。 相同。可能是我需要在几秒钟后添加计时器并设置焦点。

以上是关于从页面后面的代码显示时,JQuery 对话框上的按钮焦点不起作用 - asp.net / c#的主要内容,如果未能解决你的问题,请参考以下文章