大佬教程收集整理的这篇文章主要介绍了如何使用 javascript 禁用 PHP 中的按钮,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在登录页面上有一个按钮,它在 php 端运行良好,但我想在点击后禁用该按钮
我试过回显带有禁用它的功能的脚本。
我使用相同的方法禁用其他按钮并且它可以工作,当我将 onclick 添加到按钮时它可以工作但它禁用了提交功能。
如何在保留提交功能的同时使用 JavaScript 功能?
注意:JavaScript 文件和 php 文件是两个不同的文件。
<?php
if (isset($_POST)) {
if (!empty($_POST['username']) && !empty($_POST['password'])) {
echo '<script type="text/JavaScript">','console.log("Were here")','disableLoginbutton()','</script>';
header("LOCATIOn:NextPage.php");
}
}
}
?>
HTML
<form>
<div class="row">
<label>Username</label>
<input type="text">
</div>
<div class="row">
<label>Username</label>
<input type="text">
</div>
<button type="submit" ID="button">
</form>
JavaScript
function disableLoginbutton(){
const loginbutton = document.getElementByID("loginbutton");
loginbutton.classList.toggle('buttonHover');
loginbutton.classList.toggle('Disabled');
console.log("Hello there");
loginbutton.Disabled = true;
}
CSS
button:Disabled{
cursor: none;
BACkground-color: rgb(231,231,231);
color:rgb(53,53,53);
}
.Disabled {
pointer-events: none;
opacity: 0.3;
}
您需要在客户端对按下要禁用的按钮做出反应,而不是在服务器端。
function onSubmit(E) {
disableLoginButton();
}
function disableLoginButton() {
const loginButton = document.getElementById("login-button");
console.log("Hello there");
loginButton.disabled = true;
}
document.getElementById('login-form').addEventListener('submit',onSubmit)
button:disabled {
cursor: none;
BACkground-color: rgb(231,231,231);
color: rgb(53,53,53);
pointer-events: none;
opacity: 0.3;
}
<form id="login-form">
<div class="row">
<label>Username</label>
<input type="text">
</div>
<div class="row">
<label>password</label>
<input type="password">
</div>
<button type="submit" id="login-button">Submit</button>
</form>
,
您无法使用表单提交回显存在于不同 php 文件中的脚本(您可以使用 Ajax)
做这样的事情
<?php
if (isset($_POST)) {
if (!empty($_POST['username']) && !empty($_POST['password'])) {
echo '<script type="text/javascript">','console.log("Were here")','disableLoginButton()','</script>';
header("LOCATIOn:NextPage.php");//Actually no point of disabling button when u do this
}
}
}
?>
<form action="" method="post"><!-- SubmitTing to same page-->
<div class="row">
<label>Username</label>
<input type="text">
</div>
<div class="row">
<label>Username</label>
<input type="text">
</div>
<button type="submit" id="loginButton">
</form>
<script>
function disableLoginButton(){
const loginButton = document.getElementById("loginButton");
loginButton.classList.toggle('buttonHover');
loginButton.classList.toggle('disabled');
console.log("Hello there");
loginButton.disabled = true;
}
</script>
将 php 脚本保存在同一页面并将表单提交到同一页面将允许 php 文件访问您的案例中的 html 元素和 javascript 函数disableLoginButton()
如果您希望将表单提交到某个其他页面(后端页面)并且您想禁用 button
,则没有意义,因为 表单提交实际上重定向到操作页面 所以您的登录页面将不再可见。但是,您可以通过其他一些方法来做到这一点。
这就是我目前的观点。
以上是大佬教程为你收集整理的如何使用 javascript 禁用 PHP 中的按钮全部内容,希望文章能够帮你解决如何使用 javascript 禁用 PHP 中的按钮所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。