gpt4 book ai didi

javascript - 两个引用类似功能的按钮不适用于 html 和 javascript ajax

转载 作者:行者123 更新时间:2023-12-02 18:21:19 24 4
gpt4 key购买 nike

我有一个问题,我确信这是一个基本问题,但似乎无法在任何地方找到答案。我有一个带有两个按钮的 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、表单 id、表单名称(全部从 <!--Login--><!--Contact--> )。当我加载页面时,我注意到第一个(登录)按钮工作得很好,但是当我关闭其对话框并按下第二个(联系)按钮时,什么也不会发生,但我的网址上会显示以下内容:

...login.php#contactmodal

(相对于 ...login.php 的原始 URL)

有人可以告诉我发生了什么事吗?为什么我的第二个按钮不起作用?另外,因为我是初学者,所以也欢迎任何有关风格或语法的意见。

最佳答案

您有 2 个具有相同 ID 的元素 modaltrigger,元素的 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" });

关于javascript - 两个引用类似功能的按钮不适用于 html 和 javascript ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18797294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com