Я написал простое поле ввода с кнопкой. Код приведен ниже.
form {
width: 50%;
}
/* Style the search field */
.add-on {
position: relative;
}
.add-on input {
width: 100%;
border-radius: 0;
}
.add-on button {
position: absolute;
border-radius: 0;
top: 2px;
right: 3px;
height: 33px;
width: 60px;
z-index: 2;
font-size: 14px;
padding: 5px;
}<link href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel = "stylesheet">
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<form class = "navbar-form my-2 my-lg-0 ml-auto" role = "search">
<div class = "input-group add-on">
<input type = "text" class = "form-control" placeholder = "Search" name = "srch-term" id = "srch-term">
<button class = "btn btn-primary" type = "submit">Search</button>
</div>
</form>Как видите, когда я нажимаю в поле ввода, кнопка исчезает. Когда я щелкаю за пределами этой формы, она снова появляется. Но я не хочу, чтобы он исчезал при нажатии на поле ввода. Как я могу это сделать?
Хм. Я думаю, что кнопка исчезает, потому что. текстовое поле расширяется за кнопку. Уменьшите ширину ввода текста, и это может сработать...
Кнопка все еще там - она просто закрыта полем ввода. Вы можете видеть, что поле ниже обычно является кнопкой, а затем оно появляется сверху, когда вы нажимаете на него.
@AjayKulkarni не рекомендуется показывать кнопка поиска с помощью position: absolute в поле input. Предположим, у вас есть длинный контент в вашем input, тогда ваш кнопка поиска будет перекрывать ваш контент. В настоящее время это происходит в вашем коде.
Можете ли вы объяснить, ПОЧЕМУ вы хотите, чтобы кнопка перекрывала ввод? Может быть, мы придумаем другое решение.
Добавьте padding-right: 45px; к .add-on input, чтобы решить проблему, которую описывает @HassanSiddiqui.



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вы должны установить position: relative на кнопку
form {
width: 50%;
}
/* Style the search field */
.add-on {
position: relative;
}
.add-on input {
width: 100%;
border-radius: 0;
}
.add-on button {
position: relative;
border-radius: 0;
top: 2px;
right: 3px;
height: 33px;
width: 60px;
z-index: 2;
font-size: 14px;
padding: 5px;
}<link href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel = "stylesheet">
<script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class = "navbar-form my-2 my-lg-0 ml-auto" role = "search">
<div class = "input-group add-on">
<input type = "text" class = "form-control" placeholder = "Search" name = "srch-term" id = "srch-term">
<button class = "btn btn-primary" type = "submit">Search</button>
</div>
</form>что выводит кнопку за пределы поля ввода. Я хочу, чтобы он оставался внутри поля ввода.
используйте z-index во время фокусировки
input:focus ~ .btn-primary {
z-index: 100;
}
form {
width: 50%;
}
/* Style the search field */
.add-on {
position: relative;
}
.add-on input {
width: 100%;
border-radius: 0;
}
.add-on button {
position: absolute;
border-radius: 0;
top: 2px;
right: 3px;
height: 33px;
width: 60px;
z-index: 2;
font-size: 14px;
padding: 5px;
}
input:focus ~ .btn-primary {
z-index: 100;
}<link href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel = "stylesheet">
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<form class = "navbar-form my-2 my-lg-0 ml-auto" role = "search">
<div class = "input-group add-on">
<input type = "text" class = "form-control" placeholder = "Search" name = "srch-term" id = "srch-term">
<button class = "btn btn-primary" type = "submit">Search</button>
</div>
</form>Установите z-index: 1 на элемент #srch-term
Измените z-index в правиле CSS .add-on button на 3.
.add-on button {
position: absolute;
border-radius: 0;
top: 2px;
right: 3px;
height: 33px;
width: 60px;
z-index: 3;
font-size: 14px;
padding: 5px;
}
Просто дайте более высокое значение z-index вашей кнопки. Вероятно, подсветка ввода, которая помещает на него div
form {
width: 50%;
}
/* Style the search field */
.add-on {
position: relative;
}
.add-on input {
width: 100%;
border-radius: 0;
}
.add-on button {
position: absolute;
border-radius: 0;
top: 2px;
right: 3px;
height: 33px;
width: 60px;
z-index: 5;
font-size: 14px;
padding: 5px;
}<link href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel = "stylesheet">
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<form class = "navbar-form my-2 my-lg-0 ml-auto" role = "search">
<div class = "input-group add-on">
<input type = "text" class = "form-control" placeholder = "Search" name = "srch-term" id = "srch-term">
<button class = "btn btn-primary" type = "submit">Search</button>
</div>
</form>Как говорят другие, поле ввода закрывает кнопку, когда она сфокусирована. Попробуйте установить постоянный z-индекс для ввода и кнопки и сделать z-индекс для кнопки выше, чем для ввода.
form {
width: 50%;
}
/* Style the search field */
.add-on {
position: relative;
}
.add-on input {
width: 100%;
border-radius: 0;
z-index: 1020;
}
.add-on button {
position: absolute;
border-radius: 0;
top: 2px;
right: 3px;
height: 33px;
width: 60px;
z-index: 2;
font-size: 14px;
padding: 5px;
z-index: 1021;
} <link href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel = "stylesheet">
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<form class = "navbar-form my-2 my-lg-0 ml-auto" role = "search">
<div class = "input-group add-on">
<input type = "text" class = "form-control" placeholder = "Search" name = "srch-term" id = "srch-term">
<button class = "btn btn-primary" type = "submit">Search</button>
</div>
</form>Проблема исчезнет, если я добавлю z-index: 1 для ввода и z-index: 3 для кнопки. Не могу понять, почему, но z-index: 2 на кнопке, похоже, не работает.
просто добавьте z-index к вводу. значение больше -1 и меньше кнопки z-index
/* Style the search field */
.add-on input {
width: 100%;
border-radius: 0;
z-index: 0
}
.add-on button {
position: absolute;
border-radius: 0;
top: 2px;
right: 3px;
height: 33px;
width: 60px;
z-index: 2;
font-size: 14px;
padding: 5px;
}
Это ничем не отличается от любого другого ответа.
Solution 1 - Change
z-index: 2toz-index: 3in.add-on button, but if your content increase the search button will overlap with content.
form {
width: 50%;
}
/* Style the search field */
.add-on {
position: relative;
}
.add-on input {
width: 100%;
border-radius: 0;
}
.add-on button {
position: absolute;
border-radius: 0;
top: 2px;
right: 3px;
height: 33px;
width: 60px;
z-index: 3;
font-size: 14px;
padding: 5px;
}<link href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel = "stylesheet">
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<form class = "navbar-form my-2 my-lg-0 ml-auto" role = "search">
<div class = "input-group add-on">
<input type = "text" class = "form-control" placeholder = "Search" name = "srch-term" id = "srch-term">
<button class = "btn btn-primary" type = "submit">Search</button>
</div>
</form>Solution 2 - Remove
position: absolute,top: 2px,right: 3px,z-index: 2from.add-on buttonand updateheight: 33px;toheight: 38px;in.add-on button, it'll display search next to input field and it'll not overlap content.
form {
width: 50%;
}
/* Style the search field */
.add-on {
position: relative;
}
.add-on input {
width: 100%;
border-radius: 0;
}
.add-on button {
border-radius: 0;
height: 38px;
width: 60px;
font-size: 14px;
padding: 5px;
}<link href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel = "stylesheet">
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<form class = "navbar-form my-2 my-lg-0 ml-auto" role = "search">
<div class = "input-group add-on">
<input type = "text" class = "form-control" placeholder = "Search" name = "srch-term" id = "srch-term">
<button class = "btn btn-primary" type = "submit">Search</button>
</div>
</form>
Я получаю эту ошибку... { "message": "Ошибка скрипта.", "filename": "", "lineno": 0, "colno": 0 }