我有一个ul li键盘,该键盘首先获取4个数字,然后获取3个字母,然后将其自身禁用。但是,如果用户单击删除按钮,它将不会重新启用自身。
我想:“如果长度小于8,我只添加.removeClass('disabled')可能会起作用。但是我很确定那不是这样的。请帮帮我。
$(function() {
var $write = $('#write'),
symbols = $('.symbol'),
letters = $('.letter'),
shift = false,
capslock = false;
$('#keyboard li').click(function() {
var $this = $(this),
character = $this.html(); // If it's a lowercase letter, nothing happens to this variable
// Shift keys
if ($this.hasClass('left-shift') || $this.hasClass('right-shift')) {
$('.letter').toggleClass('uppercase');
$('.symbol span').toggle();
shift = (shift === true) ? false : true;
capslock = false;
return false;
}
// Caps lock
if ($this.hasClass('capslock')) {
$('.letter').toggleClass('uppercase');
capslock = true;
return false;
}
// Delete
if ($this.hasClass('delete')) {
var html = $write.html();
$write.html(html.substr(0, html.length - 1));
return false;
}
// Special characters
if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
if ($this.hasClass('space')) character = ' ';
if ($this.hasClass('tab')) character = "\t";
if ($this.hasClass('return')) character = "\n";
// Uppercase letter
if ($this.hasClass('uppercase')) character = character.toUpperCase();
// Remove shift once a key is clicked.
if (shift === true) {
$('.symbol span').toggle();
if (capslock === false) $('.letter').toggleClass('uppercase');
shift = false;
}
$write.html($write.html() + character);
text1 = document.getElementById('write').value;
console.log(text1);
if (text1.length >= 4) {
symbols.addClass('disabled');
letters.removeClass('disabled');
if (text1.length == 7) {
letters.addClass('disabled');
}
}
if (text1.length > 7) {
letters.removeClass('disabled');
}
// Add the character
});
});
html {
width: 100%;
height: auto;
}
body {
margin: 0;
padding: 0;
width: 100%;
height: auto;
}
.maincontainer {
background-image: url(b.png);
background-repeat: repeat-y;
background-position: right;
}
/*head*/
.nav-text {
vertical-align: middle;
color: white;
}
.row {
padding: 15px;
display: flex;
justify-content: center;
}
.back-button {
height: 100%;
}
#backer {
float: left;
width: auto;
}
.red {
background-color: darkred;
}
.header {
text-align: center;
color: white;
font-size: 25px;
float: left;
width: 90%;
}
.uk-icon {
color: white;
}
/*main*/
/* Style the tab */
.tab {
width: 100%;
overflow: hidden;
}
/* Style the buttons inside the tab */
.tab button {
width: 50%;
background-color: inherit;
float: left;
border: none;
background-color: darkred;
color: white;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
#tabone {
border-top-left-radius: 5px;
border-right: 1px solid white;
}
#tabtwo {
border-top-right-radius: 5px;
border-left: 1px solid white;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: rgb(80, 3, 3);
}
/* Create an active/current tablink class */
.tab button.active {
background-color: rgb(180, 15, 15);
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
background-color: white;
}
#London {
text-align: center;
}
#searchbutton {
background-color: darkred;
color: white;
border: 0;
border-radius: 5px;
margin-bottom: 15px;
padding-right: 20px;
}
#write {
width: 90%;
height: 300px;
background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Soyombo_red.svg/300px-Soyombo_red.svg.png) no-repeat scroll 50px 40px;
background-size: 80PX;
padding-left: 50px;
border-radius: 5%;
border: 1px solid black;
margin-bottom: 20px;
margin-top: 20px;
text-align: center;
font-size: 10vh;
}
#London>.container-fluid>h3 {
margin-top: 5%;
font-size: 4vh;
}
#keyboard {
margin: 0;
padding: 0;
list-style: none;
width: 100%;
}
#keyboard li {
font-size: 30px;
display: inline-block;
margin: 0 5px 5px 0;
width: 5%;
padding: 0;
height: 70px;
line-height: 70px;
text-align: center;
background: #fff;
border: 1px solid black;
-moz-border-radius: 5px;
-webkit-border-radius: 7PX;
;
}
#keyboard .delete {
width: 120px;
}
.lastitem {
margin-right: 0;
}
.uppercase {
text-transform: uppercase;
}
.on {
display: none;
}
#keyboard li:hover {
position: relative;
top: 1px;
left: 1px;
border-color: #e5e5e5;
cursor: pointer;
}
#container.ul {
margin: auto;
float: center;
}
#container-x {
width: 50%;
margin: auto;
}
input[type=submit] {
background: url(http://www.clker.com/cliparts/Y/3/d/w/R/r/search-icon-white-hi.png) no-repeat;
background-size: 25%;
background-position-y: 50%;
background-position-x: 10%;
}
.disabled {
opacity: 0.5;
pointer-events: none;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
Payment
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/simple-keyboard@latest/build/css/index.css">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- UIkit CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.1.3/css/uikit.min.css" />
<!-- UIkit JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.1.3/js/uikit.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.1.3/js/uikit-icons.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
</script>
<!-- My CSS & JS-->
<script src="script.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="maincontainer">
<div class="container-fluid red">
<div class="row">
<div class="col-">
<div class="back-button">
<a id="backer" class="uk-button uk-button-default" href="#"><span uk-icon="icon:arrow-left;"></span><span class="nav-text">Эхлэх</span></a>
</div>
</div>
<div class="col-xl">
<div class="header">
Тээврийн хэрэгслийн торгууль төлөх
</div>
</div>
<div class="col-">
<div class="header">
</div>
</div>
</div>
</div>
<div class="uk-container">
<img style="width: auto;">
<div class="box">
<div class="tab">
<button uk-icon="bus" class="tablinks" id="tabone" onclick="openTab(event, 'London')">ТЭЭВРИЙН ХЭРЭГСЭЛ</button>
<button class="tablinks" id="tabtwo" onclick="openTab(event, 'Paris')">ЖОЛООНЫ ҮНЭМЛЭХ</button>
</div>
<div id="London" class="tabcontent">
<div class="container-fluid">
<h3>АВТОМАШИНЫ УЛСЫН ДУГААР</h3>
<div id="input_container">
<textarea type="text" required id="write" maxLength="4" placeholder="UBA0000"></textarea>
</div>
</div>
<div>
<input type="submit" value="Хайх" id="searchbutton" class="uk-button uk-button-default" href="#"></input>
</div>
<div style="width:auto;margin: auto;" id="container">
<ul style="display:table; width:100%;" id="keyboard">
<li class="symbol"><span class="off">1</span></li>
<li class="symbol"><span class="off">2</span></li>
<li class="symbol"><span class="off">3</span></li>
<li class="symbol"><span class="off">4</span></li>
<li class="symbol"><span class="off">5</span></li>
<li class="symbol"><span class="off">6</span></li>
<li class="symbol"><span class="off">7</span></li>
<li class="symbol"><span class="off">8</span></li>
<li class="symbol"><span class="off">9</span></li>
<li class="symbol"><span class="off">0</span></li>
<li class="delete"><span class="delete"><img style="width:50%"
src="https://cdn3.iconfinder.com/data/icons/sympletts-part-1/128/backspace-256.png"></span></li>
<br>
<li class="letter disabled">Ф</li>
<li class="letter disabled">Ц</li>
<li class="letter disabled">У</li>
<li class="letter disabled">Ж</li>
<li class="letter disabled">Э</li>
<li class="letter disabled">Н</li>
<li class="letter disabled">Г</li>
<li class="letter disabled">Ш</li>
<li class="letter disabled">Ү</li>
<li class="letter disabled">З</li>
<li class="letter disabled">К</li>
<li class="letter disabled">Ъ</li><br>
<li class="letter disabled">Й</li>
<li class="letter disabled">Ы</li>
<li class="letter disabled">Б</li>
<li class="letter disabled">Ө</li>
<li class="letter disabled">А</li>
<li class="letter disabled">Х</li>
<li class="letter disabled">Р</li>
<li class="letter disabled">О</li>
<li class="letter disabled">Л</li>
<li class="letter disabled">Д</li>
<li class="letter disabled">П</li><br>
<li class="letter disabled">Я</li>
<li class="letter disabled">Ч</li>
<li class="letter disabled">Е</li>
<li class="letter disabled">Ё</li>
<li class="letter disabled">С</li>
<li class="letter disabled">М</li>
<li class="letter disabled">И</li>
<li class="letter disabled">Т</li>
<li class="letter disabled">Ь</li>
<li class="letter disabled">В</li>
<li class="letter disabled">Ю</li>
</ul>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</div>
</div>
</div>
<div id="Paris" class="tabcontent">
<h3>Paris</h3>
</div>
<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
</div>
</div>
</div>
</body>
</html>
除了缺少重要功能外,其他类型的工作都需要做。没有足够的经验来解决此问题。 (ps这个键盘实际上不关心textarea字符限制,因为它不是在按下按键时,我不知道它是什么。所以我只是作了一些即兴的工作来解决。如果您只是用键盘来写,它将最多输入4个字母,因为这是文本区域的限制。如果您使用屏幕键盘进行书写,则将获得7个帮助。)
(P.S.S:为什么最小化时,“左手”标签仍会向上攀升?)
答案 0 :(得分:1)
我想:“如果长度小于8,我只添加.removeClass('disabled')可能会起作用。但是我很确定那不是这样的。
这不起作用,因为当您处理删除按钮时,您使用了“ return false;
”,这意味着它将在此处返回并且不再继续将代码运行到.removeClass('disabled')
部分。
我的解决方案是您需要处理删除功能中禁用的添加和删除,如下所示:
// Delete
if ($this.hasClass('delete')) {
var html = $write.html();
$write.html(html.substr(0, html.length - 1));
//get text1 value from textarea
text1 = document.getElementById('write').value;
//When length < 4, just enable numbers, disable letters
if (text1.length < 4)
{
symbols.removeClass('disabled');
letters.addClass('disabled');
}
//When length >= 4, handle like your current code
if (text1.length >= 4)
{
symbols.addClass('disabled');
letters.removeClass('disabled');
if(text1.length == 7){
letters.addClass('disabled');
}
}
return false;
}
因此,在删除部分中,您将处理虚拟键盘上的禁用。
从那以后,您可以在代码中删除此部分:
if (text1.length > 7) { letters.removeClass('disabled'); }
对于物理键盘输入,如果您不希望用户使用它,则可以设置
maxLength="0"
在文本区域部分。
<textarea type="text" required id="write" maxLength="0" placeholder="UBA0000"></textarea>
请查看下面的演示以了解其工作原理。
快乐的编码时间:D。
$(function(){
var $write = $('#write'),
symbols = $('.symbol'),
letters = $('.letter'),
shift = false,
capslock = false;
$('#keyboard li').click(function(){
var $this = $(this),
character = $this.html(); // If it's a lowercase letter, nothing happens to this variable
// Shift keys
if ($this.hasClass('left-shift') || $this.hasClass('right-shift')) {
$('.letter').toggleClass('uppercase');
$('.symbol span').toggle();
shift = (shift === true) ? false : true;
capslock = false;
return false;
}
// Caps lock
if ($this.hasClass('capslock')) {
$('.letter').toggleClass('uppercase');
capslock = true;
return false;
}
// Delete
if ($this.hasClass('delete')) {
var html = $write.html();
$write.html(html.substr(0, html.length - 1));
//get text1 value from textarea
text1 = document.getElementById('write').value;
//When length < 4, just enable numbers, disable letters
if (text1.length < 4)
{
symbols.removeClass('disabled');
letters.addClass('disabled');
}
//When length >= 4, handle like your current code
if (text1.length >= 4)
{
symbols.addClass('disabled');
letters.removeClass('disabled');
if(text1.length == 7){
letters.addClass('disabled');
}
}
return false;
}
// Special characters
if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
if ($this.hasClass('space')) character = ' ';
if ($this.hasClass('tab')) character = "\t";
if ($this.hasClass('return')) character = "\n";
// Uppercase letter
if ($this.hasClass('uppercase')) character = character.toUpperCase();
// Remove shift once a key is clicked.
if (shift === true) {
$('.symbol span').toggle();
if (capslock === false) $('.letter').toggleClass('uppercase');
shift = false;
}
$write.html($write.html() + character);
text1 = document.getElementById('write').value;
console.log(text1);
if (text1.length >= 4)
{
symbols.addClass('disabled');
letters.removeClass('disabled');
if(text1.length == 7){
letters.addClass('disabled');
}
}
// Add the character
});
});
html{
width: 100%;
height: auto;
}
body{
margin: 0;
padding: 0;
width: 100%;
height: auto;
}
.maincontainer{
background-image: url(b.png);
background-repeat:repeat-y;
background-position: right;
}
/*head*/
.nav-text
{
vertical-align: middle;
color: white;
}
.row
{
padding: 15px;
display: flex;
justify-content: center;
}
.back-button
{
height: 100%;
}
#backer
{
float: left;
width: auto;
}
.red
{
background-color: darkred;
}
.header
{
text-align: center;
color: white;
font-size:25px;
float: left;
width:90%;
}
.uk-icon
{
color:white;
}
/*main*/
/* Style the tab */
.tab {
width: 100%;
overflow: hidden;
}
/* Style the buttons inside the tab */
.tab button {
width: 50%;
background-color: inherit;
float: left;
border: none;
background-color: darkred;
color: white;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
#tabone
{
border-top-left-radius: 5px;
border-right: 1px solid white;
}
#tabtwo
{
border-top-right-radius: 5px;
border-left: 1px solid white;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: rgb(80, 3, 3);
}
/* Create an active/current tablink class */
.tab button.active {
background-color: rgb(180, 15, 15);
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
background-color: white;
}
#London
{
text-align: center;
}
#searchbutton
{
background-color: darkred;
color: white;
border:0;
border-radius: 5px;
margin-bottom: 15px;
padding-right:20px;
}
#write
{
width: 90%;
height:300px;
background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Soyombo_red.svg/300px-Soyombo_red.svg.png) no-repeat scroll 50px 40px;
background-size: 80PX;
padding-left:50px;
border-radius: 5%;
border: 1px solid black;
margin-bottom: 20px;
margin-top:20px;
text-align: center;
font-size:10vh;
}
#London > .container-fluid > h3{
margin-top:5%;
font-size: 4vh;
}
#keyboard {
margin: 0;
padding: 0;
list-style: none;
width: 100%;
}
#keyboard li {
font-size: 30px;
display: inline-block;
margin: 0 5px 5px 0;
width: 5%;
padding: 0;
height: 70px;
line-height: 70px;
text-align: center;
background: #fff;
border: 1px solid black;
-moz-border-radius: 5px;
-webkit-border-radius: 7PX;;
}
#keyboard .delete {
width: 120px;
}
.lastitem {
margin-right: 0;
}
.uppercase {
text-transform: uppercase;
}
.on {
display: none;
}
#keyboard li:hover {
position: relative;
top: 1px;
left: 1px;
border-color: #e5e5e5;
cursor: pointer;
}
#container.ul{
margin:auto;
float: center;
}
#container-x
{
width: 50%;
margin: auto;
}
input[type=submit]
{
background:url(http://www.clker.com/cliparts/Y/3/d/w/R/r/search-icon-white-hi.png) no-repeat;
background-size: 25%;
background-position-y: 50%;
background-position-x: 10%;
}
.disabled
{
opacity: 0.5;
pointer-events: none;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
Payment
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/simple-keyboard@latest/build/css/index.css">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- UIkit CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.1.3/css/uikit.min.css" />
<!-- UIkit JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.1.3/js/uikit.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.1.3/js/uikit-icons.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
</script>
<!-- My CSS & JS-->
<script src="script.js"></script>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div class="maincontainer">
<div class="container-fluid red">
<div class="row">
<div class="col-">
<div class="back-button">
<a id="backer" class="uk-button uk-button-default" href="#"><span uk-icon="icon:arrow-left;"></span><span class="nav-text">Эхлэх</span></a>
</div>
</div>
<div class="col-xl">
<div class="header">
Тээврийн хэрэгслийн торгууль төлөх
</div>
</div>
<div class="col-">
<div class="header">
</div>
</div>
</div>
</div>
<div class="uk-container">
<img style="width: auto;">
<div class="box">
<div class="tab">
<button uk-icon="bus" class="tablinks" id="tabone" onclick="openTab(event, 'London')">ТЭЭВРИЙН ХЭРЭГСЭЛ</button>
<button class="tablinks" id="tabtwo" onclick="openTab(event, 'Paris')">ЖОЛООНЫ ҮНЭМЛЭХ</button>
</div>
<div id="London" class="tabcontent">
<div class="container-fluid">
<h3>АВТОМАШИНЫ УЛСЫН ДУГААР</h3>
<div id="input_container">
<textarea type="text" required id="write" maxLength="0" placeholder="UBA0000"></textarea>
</div>
</div>
<div>
<input type="submit" value="Хайх" id="searchbutton" class="uk-button uk-button-default" href="#"></input>
</div>
<div style="width:auto;margin: auto;" id="container">
<ul style="display:table; width:100%;" id="keyboard">
<li class="symbol"><span class="off">1</span></li>
<li class="symbol"><span class="off">2</span></li>
<li class="symbol"><span class="off">3</span></li>
<li class="symbol"><span class="off">4</span></li>
<li class="symbol"><span class="off">5</span></li>
<li class="symbol"><span class="off">6</span></li>
<li class="symbol"><span class="off">7</span></li>
<li class="symbol"><span class="off">8</span></li>
<li class="symbol"><span class="off">9</span></li>
<li class="symbol"><span class="off">0</span></li>
<li class="delete"><span class="delete"><img style="width:50%"
src="https://cdn3.iconfinder.com/data/icons/sympletts-part-1/128/backspace-256.png"></span></li>
<br>
<li class="letter disabled">Ф</li>
<li class="letter disabled">Ц</li>
<li class="letter disabled">У</li>
<li class="letter disabled">Ж</li>
<li class="letter disabled">Э</li>
<li class="letter disabled">Н</li>
<li class="letter disabled">Г</li>
<li class="letter disabled">Ш</li>
<li class="letter disabled">Ү</li>
<li class="letter disabled">З</li>
<li class="letter disabled">К</li>
<li class="letter disabled">Ъ</li><br>
<li class="letter disabled">Й</li>
<li class="letter disabled">Ы</li>
<li class="letter disabled">Б</li>
<li class="letter disabled">Ө</li>
<li class="letter disabled">А</li>
<li class="letter disabled">Х</li>
<li class="letter disabled">Р</li>
<li class="letter disabled">О</li>
<li class="letter disabled">Л</li>
<li class="letter disabled">Д</li>
<li class="letter disabled">П</li><br>
<li class="letter disabled">Я</li>
<li class="letter disabled">Ч</li>
<li class="letter disabled">Е</li>
<li class="letter disabled">Ё</li>
<li class="letter disabled">С</li>
<li class="letter disabled">М</li>
<li class="letter disabled">И</li>
<li class="letter disabled">Т</li>
<li class="letter disabled">Ь</li>
<li class="letter disabled">В</li>
<li class="letter disabled">Ю</li>
</ul>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</div>
</div>
</div>
<div id="Paris" class="tabcontent">
<h3>Paris</h3>
</div>
<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
</div>
</div>
</div>
</body>
</html>