2016-11-21 63 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调用。我们不能只为你写这些,你需要找出你想要存储这些细节的地方,以及当按钮被点击时你想如何收集它们。

关于这个问题,我怀疑你已经得到了答案。所以我会将其中一个标记为答案,对下一个问题进行刺探,并在需要时创建一个新问题。