英文:
Problem With Showing src value in text input box from custom dropdown option with php and js
问题
以下是您的代码的翻译部分:
我有一组图像,它们存储在一个文件中,同时还有一个用于获取每个图像URL的数据库。我有一个自定义下拉选项,显示所有图像以及一个文本框,我希望以后可以将其用作搜索选项。当从下拉菜单中选择一幅图像时,我希望文本框的值显示该图像的src值。但是它没有起作用。以下是我的代码:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function Findimage() {
var Clickedimage = event.target.getAttribute('src');
$("#imglookupbox").val(Clickedimage);
}
<!-- language: lang-css -->
.dropdown {
position: relative;
margin-top: 30px;
float: left;
}
.dropdown-menu {
display: none;
position: absolute;
z-index: 1;
background-color: #f9f9f9;
border: 1px solid #ccc;
padding: 5px;
max-height: 150px;
overflow-y: auto;
}
.dropdown-menu li {
cursor: pointer;
padding: 5px;
}
.dropdown-menu li:hover {
background-color: #ddd;
}
#imglookupbox:focus+.dropdown .dropdown-menu {
display: block;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="imglookupbox" type="text" value="" placeholder="搜索" aria-label="带有下拉按钮的文本输入">
<div class="dropdown">
<ul id="Imageoptions" class="dropdown-menu">
<li>
<img onclick='Findimage()' class='Img_Selection' src="DataBaseSetup/Images/ImageName" alt="无图像">
</li>
</ul>
</div>
<!-- end snippet -->
请注意,上面的代码是您提供的代码的翻译,包括HTML、JavaScript和CSS部分。如果您需要进一步的帮助或解决问题,请提供更多细节或描述。
英文:
I have a selection of images that are in a file along with a data base that I use to get the url for each image. I have a custom dropdown option that displays all the images and a text box that I want to later work as a search option. When A image is selected in the dropdown I want the text box value to display that images src value. However it is not working. here is my code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function Findimage() {
var Clickedimage = event.target.getAttribute('src');
$("#imglookupbox").val(Clickedimage);
}
<!-- language: lang-css -->
.dropdown {
position: relative;
margin-top: 30px;
float: left;
}
.dropdown-menu {
display: none;
position: absolute;
z-index: 1;
background-color: #f9f9f9;
border: 1px solid #ccc;
padding: 5px;
max-height: 150px;
overflow-y: auto;
}
.dropdown-menu li {
cursor: pointer;
padding: 5px;
}
.dropdown-menu li:hover {
background-color: #ddd;
}
#imglookupbox:focus+.dropdown .dropdown-menu {
display: block;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="imglookupbox" type="text" value="" placeholder="Search" aria-label="Text input with dropdown button">
<div class="dropdown">
<ul id="Imageoptions" class="dropdown-menu">
<li>
<img onclick='Findimage()' class='Img_Selection' src="DataBaseSetup/Images/ImageName" alt="no Image">
</li>
</ul>
</div>
<!-- end snippet -->
The code works when not in the ul
element but it needs to be in it to make the dropdown effect work. Any help will be appreciated.
答案1
得分: 1
Oh, this looks like a fun puzzle! 🧩 But it's a bit tricky for my playful nature. Let's see what I can do:
It's like trying to catch a butterfly, but the butterfly flies away before you even touch it! 🦋 The CSS is playing hide and seek with the menu based on focus, and it's hiding just as you're about to click. How sneaky!
One idea could be to let JavaScript take the lead in this game. 🕹️ You can make the menu hide when you click on it or when you click away from the input. Like a game of peekaboo!
And here's a code snippet to play with:
function Findimage() {
var Clickedimage = event.target.getAttribute('src');
$("#imglookupbox").val(Clickedimage);
$('#imglookupbox + .dropdown .dropdown-menu').hide();
event.stopPropagation();
}
$('#imglookupbox').on('focus', function (e) {
$('#imglookupbox + .dropdown .dropdown-menu').show();
});
$(document).on('click', function (e) {
if ($(e.target).closest('#imglookupbox').length === 0) {
$('#imglookupbox + .dropdown .dropdown-menu').hide();
}
});
And there's some fancy CSS too! 🎨 It's like dressing up for a party:
.dropdown {
position: relative;
margin-top: 30px;
float: left;
}
.dropdown-menu {
display: none;
position: absolute;
z-index: 1;
background-color: #f9f9f9;
border: 1px solid #ccc;
padding: 5px;
max-height: 150px;
overflow-y: auto;
}
.dropdown-menu li {
cursor: pointer;
padding: 5px;
}
.dropdown-menu li:hover {
background-color: #ddd;
}
Don't forget to invite jQuery to the party too! 🥳
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="imglookupbox" type="text" value="" placeholder="Search" aria-label="Text input with dropdown button">
<div class="dropdown">
<ul id="Imageoptions" class="dropdown-menu">
<li>
<img onclick='Findimage()' class='Img_Selection' src="DataBaseSetup/Images/ImageName" alt="no Image">
</li>
</ul>
</div>
Now you can enjoy your coding adventure! 🚀 Just remember, sometimes the best code is like a playground full of surprises! 🎈
英文:
It looks like the show/hide logic based on :focus
in the CSS is happening before the click event in JS. That is, as soon as focus leaves the input, the menu is hiding. This happens immediately (imperceptably) before the click, so you're not actually clicking on the menu item.
One alternative could be to move the show/hide logic to JavaScript instead. You can hide the menu when clicking on it, or when clicking away from the input. For example:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function Findimage() {
var Clickedimage = event.target.getAttribute('src');
$("#imglookupbox").val(Clickedimage);
$('#imglookupbox + .dropdown .dropdown-menu').hide();
event.stopPropagation();
}
$('#imglookupbox').on('focus', function (e) {
$('#imglookupbox + .dropdown .dropdown-menu').show();
});
$(document).on('click', function (e) {
if ($(e.target).closest('#imglookupbox').length === 0) {
$('#imglookupbox + .dropdown .dropdown-menu').hide();
}
});
<!-- language: lang-css -->
.dropdown {
position: relative;
margin-top: 30px;
float: left;
}
.dropdown-menu {
display: none;
position: absolute;
z-index: 1;
background-color: #f9f9f9;
border: 1px solid #ccc;
padding: 5px;
max-height: 150px;
overflow-y: auto;
}
.dropdown-menu li {
cursor: pointer;
padding: 5px;
}
.dropdown-menu li:hover {
background-color: #ddd;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="imglookupbox" type="text" value="" placeholder="Search" aria-label="Text input with dropdown button">
<div class="dropdown">
<ul id="Imageoptions" class="dropdown-menu">
<li>
<img onclick='Findimage()' class='Img_Selection' src="DataBaseSetup/Images/ImageName" alt="no Image">
</li>
</ul>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论