简体   繁体   English

如何将类添加到html中的Submit类型的输入中,例如添加后将不会将其删除?

[英]How can I add a class to an input of type submit in html such as it won't be removed once added?

I am trying to add a class to an input of type submit I tried using javascript input is inside a form 我正在尝试向使用javascript输入尝试过的类型为Submit的输入添加类

<input type='submit' name='$emri" . "$mbiemri" . "submit" . "$depart'" . " value='Vlereso' onclick='disable(this)'>

where disable is a function : 其中disable是一个函数:

 function disable(element)
          {
            element.classList.add('disabled');
          }

when the input is clicked it adds the class but when the action is sent the class is removed again , what I want to do is when I click the input I don't want it to be clickable anymore so I am trying to add class disabled and I don't want it to be removed , is it possible to do it with JS or PHP is needed ? 当单击输入时,它添加了类,但是当发送动作时,再次删除了该类,我想做的是,当我单击输入时,我不想再单击它,因此我尝试添加禁用的类而且我不希望将其删除,是否需要使用JS或PHP来完成?

When you click on the submit button, it will submit the form data (if present) to the target url, which is the current url by default. 当您单击提交按钮时,它将提交表单数据(如果存在)到目标URL,默认情况下是当前URL。 What you see is that the page is being reloaded afresh, so the class that you added will not be there anymore, because technically, it's a new page. 您会看到页面正在重新加载,因此您添加的类将不再存在,因为从技术上讲,它是一个新页面。

What you need to do is to disable the button right at the page load, if it's not to be used again. 如果不打算再次使用该按钮,则需要在页面加载时禁用该按钮。 (Arguably you could remove the button altogether, because it's no use to have a disabled button that is never to be used again.) (可以说,您可以完全删除该按钮,因为禁用的按钮不再使用是没有用的。)

To disable the <input ... , just add the attribute disabled to the tag. 要禁用<input ... ,只需添加属性disabled的标签。

You can store a boolean inside sessionStorage and when the page loads check if the boolean is true and if so disable the button. 您可以将一个布尔值存储在sessionStorage并在页面加载时检查布尔值是否为true,如果是,则禁用按钮。

sessionStorage will delete itself once the window gets closed. 窗口关闭后,sessionStorage将自行删除。

Example: 例:

pagescript.js: pagescript.js:

let shouldDisableInput = sessionStorage.getItem('shouldDisableInput')
if(shouldDisableInput !== undefined){
    $('#input-id').classList.add('disabled') //or $('#input-id').prop('disabled', true);
}
function disable(element){
    element.classList.add('disabled');
    sessionStorage.setItem('shouldDisableInput', 'true')
}

page.html: page.html中:

<input id="input-id" type='submit' name='$emri" . "$mbiemri" . "submit" . "$depart'" . " value='Vlereso' onclick='disable(this)'>

don't forget to add the pagescript.js inside the body of the html. 不要忘记在HTML正文中添加pagescript.js

More info on sessionStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage 有关sessionStorage的更多信息: https : //developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

您可以从php页面发送具有某些值的隐藏变量,并在html页面中检查该变量是否存在,然后打印disable标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM