2016-11-21 11 views
0

以下のコードに問題があります。 2番目のボタンは動作しないことを示します。それは何も開かない。 問題は、私がクラスに変更した場合は必ず表示されます。両方とも動作しません。 そして、私はそれぞれの行のテキストをしたいと思いますが任意のマージンなしで残されるが、私は、ボタンの属性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として2回使用されています。代わりにクラスを使用してください。 – Malk

答えて

0

変更は、uは上に同じ名前で複数のIDを使用するカント、それを固定されていませんでしたあなたのウェブサイト、代わりにクラスを使用することができます。例

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

ためと示唆された通りのjsにuがクラス

$(".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"); 
}); 

、あなたはより多くの情報を提供する必要があります。 「Show」ボタンを押すたびにカスタム詳細がダイアログに表示されるようにするには、その詳細をいくつかの場所から提供する必要があります。行にdata属性(<tr data-comments="">など)を使用するか、データベースへのAJAX呼び出しを行うことができます。そのような詳細を保存する場所と、ボタンがクリックされたときに収集する方法を把握する必要があります。

この質問に関しては、あなたは答えがあると思う。だから私は答えとしてマークし、次の問題で刺すようにし、必要に応じて新しい質問を作成します。

関連する問題