2016-11-21 66 views
0

下面的代碼存在問題。第二個按鈕顯示它不起作用。它沒有打開任何東西。 問題是肯定顯示..如果我改變它類,都不工作。 而且我會在每一行中喜歡的文字留下沒有任何餘量,但我不能固定在它的按鈕id屬性jquery html上的多個按鈕

<!DOCTYPE html> 
<html> 

    <body> 
    <!doctype html> 
    <html lang="en"> 

    <head> 
     <meta charset="utf-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <title>Cafeteria Orders Management System</title> 
     <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
     <link rel="stylesheet" href="/resources/demos/style.css"> 
     <style> 
     label, 
     input { 
      display: block; 
     } 

     input.text { 
      margin-bottom: 12px; 
      width: 95%; 
      padding: .4em; 
     } 

     fieldset { 
      padding: 0; 
      border: 0; 
      margin-top: 25px; 
     } 

     td { 
      max-width: 120px; 
      white-space: nowrap; 
     } 

     h1 { 
      font-size: 1.2em; 
      margin: .6em 0; 
     } 

     div#users-contain { 
      width: 300px; 
      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: 9px solid #eee; 
      padding: .6em 120px; 
      text-align: left; 
     } 

     .ui-dialog .ui-state-error { 
      padding: .3em; 
     } 

     .validateTips { 
      border: 1px solid transparent; 
      padding: 0.3em; 
     } 

     </style> 
     <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
     <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
     <script> 
     $(function() { 
      var dialog, form, 

      // From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29 
      emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-][email protected][a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/, 
      name = $("#name"), 
      email = $("#email"), 
      password = $("#password"), 
      allFields = $([]).add(name).add(email).add(password), 
      tips = $(".validateTips"); 

      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 addUser() { 
      var valid = true; 
      allFields.removeClass("ui-state-error"); 

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

      valid = valid && checkRegexp(name, /^[a-z]([0-9a-z_\s])+$/i, "Username may consist of a-z, 0-9, underscores, spaces and must begin with a letter."); 
      valid = valid && checkRegexp(email, emailRegex, "eg. [email protected]"); 
      valid = valid && checkRegexp(password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9"); 

      if (valid) { 
       $("#users tbody").append("<tr>" + 
       "<td>" + name.val() + "</td>" + 
       "<td>" + email.val() + "</td>" + 
       "<td>" + password.val() + "</td>" + 
       "</tr>"); 
       dialog.dialog("close"); 
      } 
      return valid; 
      } 

      dialog = $("#dialog-form").dialog({ 
      autoOpen: false, 
      height: 400, 
      width: 350, 
      modal: true, 
      buttons: { 

       Ok: function() { 
       dialog.dialog("close"); 
       } 
      }, 
      close: function() { 
       form[0].reset(); 
       allFields.removeClass("ui-state-error"); 
      } 
      }); 

      form = dialog.find("form").on("submit", function(event) { 
      event.preventDefault(); 
      addUser(); 
      }); 

      $("#create-user").button().on("click", function() { 
      dialog.dialog("open"); 
      }); 
     }); 

     </script> 
    </head> 

    <body> 
     <div id="dialog-form" title="Order Details"> 
     <p class="validateTips">Spicy Sandwitch</p> 
     <p class="validateTips">More</p> 
     <form> 
      <fieldset> 
      <label for="name">More Comments</label> 
      <p class="validateTips">Sandwitch only lettuce</p> 
      <!-- Allow form submission with keyboard without duplicating the dialog button --> 
      <input type="submit" tabindex="-1" style="position:absolute; top:-1000px"> 
      </fieldset> 
     </form> 
     </div> 
     <div id="users-contain" class="ui-widget"> 
     <h1>Order List:</h1> 
     <table id="users" class="ui-widget ui-widget-content"> 
      <thead> 
      <tr class="ui-widget-header "> 
       <th>Name/Surname</th> 
       <th>Address</th> 
       <th>Telephone</th> 
       <th>Time/Date</th> 
       <th>Order Details</th> 
       <th>Done</th> 
      </tr> 
      </thead> 
      <tbody> 
      <tr> 
       </td> 
       <td>John Doe</td> 
       <td>Lykavitou 12, 2109 Aglantzia</td> 
       <td>99123456</td> 
       <td>21:00 21/11/16</td> 
       <td> 
       <button id="create-user">Show</button> 
       </td> 
       <td align="center"> 
       <input type="checkbox" class="case" name="case" value="1" /> 
      </tr> 
      <tr> 
       </td> 
       <td>Andreas Georgiou</td> 
       <td>Grigori Auxentiou 12, 2109 Aglantzia</td> 
       <td>99654789</td> 
       <td>20:00 21/11/16</td> 
       <td> 
       <button id="create-user">Show</button> 
       </td> 
       <td align="center"> 
       <input type="checkbox" class="case" name="case" value="1" /> 
      </tr> 
      </tbody> 
     </table> 
     </div> 
    </body> 

    </html> 
+2

什麼是所有''和'的? – Turnip

+0

@Turnip我使用的是在線HTML瀏覽器,每次運行代碼時,它都會在我的代碼 – htmlste

+0

中添加頭部和正文名稱,其中'create-user'用作ID兩次。改用類。 – Malk

回答

0

變化,ü不能上使用多個ID名稱相同你的網站,你可以用班級代替。例如

<button class="create-user">Show</button> 

,並在JSü必須調用的類

$(".create-user") 
+0

謝謝,你知道如何更改不同行中每個彈出窗口的內容(文本)嗎?我的意思是每個訂單的文字都不一樣 – htmlste

0

至於有人建議,你將要使用的類屬性和選擇。

工作例如:https://jsfiddle.net/Twisty/5n2h03nL/

HTML

<div id="users-contain" class="ui-widget"> 
    <h1>Order List:</h1> 
    <table id="users" class="ui-widget ui-widget-content"> 
    <thead> 
     <tr class="ui-widget-header "> 
     <th>Name/Surname</th> 
     <th>Address</th> 
     <th>Telephone</th> 
     <th>Time/Date</th> 
     <th>Order Details</th> 
     <th>Done</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
     <td>John Doe</td> 
     <td>Lykavitou 12, 2109 Aglantzia</td> 
     <td>99123456</td> 
     <td>21:00 21/11/16</td> 
     <td> 
      <button id="create-user-1" class="showDialog">Show</button> 
     </td> 
     <td align="center"> 
      <input type="checkbox" class="case" name="case" value="1" /> 
     </tr> 
     <tr> 
     <td>Andreas Georgiou</td> 
     <td>Grigori Auxentiou 12, 2109 Aglantzia</td> 
     <td>99654789</td> 
     <td>20:00 21/11/16</td> 
     <td> 
      <button id="create-user-2" class="showDialog">Show</button> 
     </td> 
     <td align="center"> 
      <input type="checkbox" class="case" name="case" value="1" /> 
     </tr> 
    </tbody> 
    </table> 
</div> 

jQuery的

$(".showDialog").button().on("click", function() { 
    dialog.dialog("open"); 
}); 

在問候你的其他評論,您需要提供更多的信息。如果您希望「顯示」按鈕每次啓動具有自定義詳細信息的對話框,則需要從某處提供這些詳細信息。您可以在行上使用data屬性,如<tr data-comments="">或對數據庫進行AJAX調用。我們不能只爲你寫這些,你需要找出你想要存儲這些細節的地方,以及當按鈕被點擊時你想如何收集它們。

關於這個問題,我懷疑你已經得到了答案。所以我會將其中一個標記爲答案,對下一個問題進行刺探,並在需要時創建一個新問題。