0

我有一个我很确定是基本的问题,但似乎无法在任何地方找到答案。我有一个带有两个按钮的 html 页面。理想情况下,我会让他们显示/做两件不同的事情(都是弹出窗口),但我发现当他们共享完全相同的代码时,我什至无法让第二个按钮工作。

这是代码(我省略了css):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf=8"/>
<center>
    <nav>
        <a href="#loginmodal" class="flatbtn" id="modaltrigger">LOGIN</a>
        <a href="#contactmodal" class="flatbtn" id="modaltrigger">CONTACT</a>
    </nav>
</center>

<!--script for checklogin--> 
<script type = "text/javascript">

    function checkLogin(){
        //All this (index.php, validate.js, and login.php) taken from: /*http://stackoverflow.com/questions/15513031/authentication-using-ajax-php*/  
        var u = document.getElementById("username").value;
        var p = document.getElementById("password").value;
        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                if(xmlhttp.responseText == u){
                    alert(':)');
                }
                else{
                    alert(':(');               
                }
            }
        }
        xmlhttp.open("GET", "checkpw.php?u=" + u + "&p=" + p, true);
        xmlhttp.send(); 
    }
    function newUser(){
        location.href = 'signup.php';
    }
</script>
</head>

<body>
<center>

<!--Login-->
<div id="loginmodal" style="display:none;">
  <logh1>Sign In</logh1>
  <form id="loginform" name="loginform" method="post" action="index.html">
    <label for="username"><h2>Username:</h2></label>
    <input type="text" name="username" id="username" class="txtfield" tabindex="1">
    <label for="password"><h2>Password:</h2></label>
    <input type="password" name="password" id="password" class="txtfield" tabindex="2">

  <div class="center">

  <input type="submit" name="loginbtn" id="loginbtn" class="flatbtn-blu hidemodal" value="Log In" onclick="checkLogin(); return false;" tabindex="3">
  <input type="submit" name="newusrbtn" id="create-user"" class="flatbtn-blu hidemodal" value="New User" onclick="newUser();" tabindex="4">
  </div>
  </form>
</div>

<!--Contact-->
<div id="contactmodal" style="display:none;">
  <logh1>Sign In</logh1>
  <form id="contactform" name="contactform" method="post" action="index.html">
    <label for="username"><h2>Username:</h2></label>
    <input type="text" name="username" id="username" class="txtfield" tabindex="1">
    <label for="password"><h2>Password:</h2></label>
    <input type="password" name="password" id="password" class="txtfield" tabindex="2">

  <div class="center">

  <input type="submit" name="loginbtn" id="loginbtn" class="flatbtn-blu hidemodal" value="Log In" onclick="checkLogin(); return false;" tabindex="3">
  <input type="submit" name="newusrbtn" id="create-user"" class="flatbtn-blu hidemodal" value="New User" onclick="newUser();" tabindex="4">
  </div>
  </form>
</div>
</center>

<script type="text/javascript">
    $(function(){
      $('#loginform').submit(function(e){
        return false;
      });

    $('#contactform').submit(function(e){
        return false;
      });

      $('#modaltrigger').leanModal({ top: 110, overlay: 0.45, closeButton: ".hidemodal" });
    });
</script>
</body>
</html>

我所做的只是为第二个(联系人)按钮更改以下内容:div id、form id、form name(全部从<!--Login-->to <!--Contact-->)。当我加载我的页面时,我注意到第一个(登录)按钮工作得很好,但是当我关闭它的对话框并按下第二个(联系人)按钮时,什么都不会发生,但以下内容会显示在我的 url 上:

...login.php#contactmodal

(与 ...login.php 的原始网址相比)

有人可以告诉我发生了什么,为什么我的第二个按钮不起作用?另外,因为我是初学者,所以对风格或语法的任何意见都值得赞赏。

4

1 回答 1

2

您有 2 个具有相同 IDmodaltrigger的元素,元素的 ID 在页面中必须是唯一的。

改用类属性对相似的元素进行分组。

    <a href="#loginmodal" class="flatbtn modaltrigger">LOGIN</a>
    <a href="#contactmodal" class="flatbtn modaltrigger">CONTACT</a>

然后

$('.modaltrigger').leanModal({ top: 110, overlay: 0.45, closeButton: ".hidemodal" });
于 2013-09-14T01:11:32.527 回答