2011-03-18 43 views
38

飛我有什麼,我懷疑是一個簡單的問題,搜索後我懷疑回答我的問題是沒有的,但仔細檢查...jQuery的用戶界面對話框 - 在沒有DIV

是它可以使用Jquery ui對話框設置DIV嗎?

即而非..

$(function() { $("#dialog").dialog();});...

<div id="dialog" title="Do I really need this?">This is a lot of typing just to pass on a simple message</div>

是否有可能只是有類似...

$(function(quickly) { $("#dialog").dialog('this would be much easier');});...

,並提供一個可快速調用它( )需要時輸入東西。

您可能會注意到,我混淆了Javascript的方式,並通過試驗和錯誤來盡我所能打出一個適用於我的解決方案。

我想,我問,如果我能以某種方式以類似的方式使用對話框...

alert("this is simple")

...所以我每當我需要調用警報用戶,而不是通過網址點擊或按鈕按下。

希望以上是有道理的,我會懷疑我將不得不堅持使用醜陋的標準警報,但是請讓我知道是否有簡單的解決方案。

感謝

+0

再上面,我敢肯定,我鍵入它,但它應該已經閱讀

This is a lot of typing just to pass on a simple message
JTC 2011-03-18 16:47:15

+0

固定它的亞 – 2011-03-18 16:58:52

回答

1

什麼提示

但是你認爲加入一個div是寫了很多

你不會喜歡這些

http://trentrichardson.com/Impromptu/index.php

http://abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/

+0

謝謝對於這個建議。我正試圖專注於jquery-ui庫。我使用了大量的php sql,並且我正在尋找javascript,這是一種非常好的將信息發回給用戶的方式。我開始使用我在網上隨機發現的很多不同的插件,現在我正試圖將jquery UI(自動完成,日期選擇器,對話框)的ramdom mish-mash壓縮。 – JTC 2011-03-18 18:03:13

+0

謝謝@zod:http://peterkellner.net/2014/05/30/simple-prompt-modal-dialog-jquery-javascript/ – 2017-08-04 16:10:50

0

您可以創建一個幫助程序方法,它將div的創建和配置抽象出來,然後調用.dialog()。這至少會消除重複代碼和/或創建多個div的需要。

2

我正在使用rails,並不喜歡有2個地方寫代碼高度依賴。 所以我修改了下面代碼中的jquery-ui對話框示例。見executaDialogModal()

<meta charset="utf-8"> 

<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="jquery-ui.js"></script> 
<link rel="stylesheet" type="text/css" media="screen" href="jquery-ui.css" /> 


<style> 
    body { font-size: 62.5%; } 
    label, input { display:block; } 
    input.text { margin-bottom:12px; width:95%; padding: .4em; } 
    fieldset { padding:0; border:0; margin-top:25px; } 
    h1 { font-size: 1.2em; margin: .6em 0; } 
    div#users-contain { width: 350px; margin: 20px 0; } 
    div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; } 
    div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; } 
    .ui-dialog .ui-state-error { padding: .3em; } 
    .validateTips { border: 1px solid transparent; padding: 0.3em; } 
</style> 


<script> 
$(function() { 

    //------------------------------------------------------------------------ 
    // variaveis globais 
    var  name = email = password = allFields = tips = null; 
    var bValid = true; 

    //------------------------------------------------------------------------ 
    function updateTips(t) { 
     tips 
      .text(t) 
      .addClass("ui-state-highlight"); 
     setTimeout(function() { 
      tips.removeClass("ui-state-highlight", 1500); 
     }, 500); 
    } 

    //------------------------------------------------------------------------ 
    function checkLength(o, n, min, max) { 
     if (o.val().length > max || o.val().length < min) { 
      o.addClass("ui-state-error"); 
      updateTips("Length of " + n + " must be between " + 
       min + " and " + max + "."); 
      return false; 
     } else { 
      return true; 
     } 
    } 

    //------------------------------------------------------------------------ 
    function checkRegexp(o, regexp, n) { 
     if (!(regexp.test(o.val()))) { 
      o.addClass("ui-state-error"); 
      updateTips(n); 
      return false; 
     } else { 
      return true; 
     } 
    } 

    //------------------------------------------------------------------------ 
    function executaDialogModal() { 
     //----TODO: verificar antes a existência de #dialog-form e destrui-lo 
     //----cria Form 
     $('body').append("\ 
      <div id='dialog-form' title='Create new user'>\ 
       <p class='validateTips'>All form fields are required.</p>\ 
       <form>\ 
       <fieldset>\ 
        <label for='name'>Name</label>\ 
        <input type='text' name='name' id='name' class='text ui-widget-content ui-corner-all' />\ 
        <label for='email'>Email</label>\ 
        <input type='text' name='email' id='email' value='' class='text ui-widget-content ui-corner-all' />\ 
        <label for='password'>Password</label>\ 
        <input type='password' name='password' id='password' value='' class='text ui-widget-content ui-corner-all' />\ 
       </fieldset>\ 
       </form>\ 
      </div>\ 
     "); 
     //----seta vars globais 
     name = $("#name"), 
     email = $("#email"), 
     password = $("#password"), 
     allFields = $([]).add(name).add(email).add(password), 
     tips = $(".validateTips"); 

     //----cria dialog 
     $("#dialog-form").dialog({ 
      autoOpen: true, 
      height: 300, 
      width: 350, 
      modal: true, 
      buttons: { 
       "Create an account": function() { 


        allFields.removeClass("ui-state-error"); 
        bValid = true; 

        bValid = bValid && checkLength(name, "username", 3, 16); 
        bValid = bValid && checkLength(email, "email", 6, 80); 
        bValid = bValid && checkLength(password, "password", 5, 16); 

        bValid = bValid && checkRegexp(name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter."); 
        // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/ 
        bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. [email protected]"); 
        bValid = bValid && checkRegexp(password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9"); 

        if (bValid) { 
         $("#users tbody").append("<tr>" + 
          "<td>" + name.val() + "</td>" + 
          "<td>" + email.val() + "</td>" + 
          "<td>" + password.val() + "</td>" + 
         "</tr>"); 
         $(this).dialog("close"); 
        } 
       }, 
       Cancel: function() { 
        $(this).dialog("close"); 
       } 
      }, 
      close: function() { 
       removeFormModal(); 
       allFields.val("").removeClass("ui-state-error"); 
      } 
     });   
    } 

    //------------------------------------------------------------------------ 
    function removeFormModal() { 
     $("#dialog-form").remove(); 
    } 

    //------------------------------------------------------------------------ 
    $("#create-user") 
     .button() 
     .click(function() { 
      executaDialogModal(); 
     }); 
}); 



</script> 

現有用戶:

名稱 電子郵件 密碼 李四 [email protected]。COM johndoe1

創建新用戶

74

我希望這可以幫助別人,你可以通過HTML直接對話框,如下所示:

$("<p>Hello World!</p>").dialog(); 

所以這樣你不不必有預先建成的股利,你可以使用:

$("<div>My div content</div>").dialog(); 

編輯:換下場標籤</div>,而不是</p>

+0

是的,謝謝:) – delphirules 2015-07-19 21:37:40

1

這對我的作品。請注意,以避免身體充滿div的,我保證使用DIV靠近後取出:

$('<div>').prop('id', '_currentDialog').text("Please enter a Comment").dialog(
    { 
     title: "Input Error", 
        close: function() { 
         $('#_currentDialog').remove(); 
        } 
    } 
); 
相關問題