簡體   English   中英

jQuery UI對話框將無法打開

[英]jQuery UI Dialog will not open

我試圖顯示一個jQuery UI對話框以響應表單輸入。 考慮:

<html>
   <head>
      <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/smoothness/jquery-ui.css" />
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> 
      <script src="checkLogin.js"></script>
   </head>
   <body>
      <form>
         <label>User Name:</label><input onkeydown="checkSubmitKey(event)"><br>
         <input id="submit" name="submit" type="button" onclick="checkLogin()" value="Submit">
      </form>
      <div id="dialog" title="Alert" style="display: none;"></div>
   </body>
</html>

其中checkLogin.js是:

function checkSubmitKey(ev) {
    if(ev.keyCode == 13) {
        checkLogin();
    }
}

function checkLogin() {
   showAlert("Hello");
}

function showAlert(str) {
    $( "#dialog" ).html(str);
    $( "#dialog" ).dialog({
        modal: true,
        title: "Alert",
        buttons: {
            "OK": function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

問題是,僅當我按下“提交”按鈕時才會顯示對話框。 如果在“用戶名”文本字段中按Enter鍵 ,則不會發生任何事情。

(如果我將功能checkLogin更改為

function checkLogin() {
   alert("ok");
   showAlert("Hello");
}

當我也按下Enter鍵時,對話框就會出現。)

您需要阻止Enter鍵的默認操作。 將JS更改為:

function checkSubmitKey(ev) {
    if(ev.keyCode == 13) {
        ev.preventDefault();
        checkLogin();
    }
}

小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM