显示来自自定义下拉选项的文本输入框中的src值的问题,使用php和js。

huangapple go评论63阅读模式
英文:

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(&#39;src&#39;);
  $(&quot;#imglookupbox&quot;).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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input id=&quot;imglookupbox&quot; type=&quot;text&quot; value=&quot;&quot; placeholder=&quot;Search&quot; aria-label=&quot;Text input with dropdown button&quot;&gt;
&lt;div class=&quot;dropdown&quot;&gt;
  &lt;ul id=&quot;Imageoptions&quot; class=&quot;dropdown-menu&quot;&gt;
    &lt;li&gt;
      &lt;img onclick=&#39;Findimage()&#39; class=&#39;Img_Selection&#39; src=&quot;DataBaseSetup/Images/ImageName&quot; alt=&quot;no Image&quot;&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

<!-- 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(&#39;src&#39;);
  $(&quot;#imglookupbox&quot;).val(Clickedimage);
  $(&#39;#imglookupbox + .dropdown .dropdown-menu&#39;).hide();
  event.stopPropagation();
}

$(&#39;#imglookupbox&#39;).on(&#39;focus&#39;, function (e) {
  $(&#39;#imglookupbox + .dropdown .dropdown-menu&#39;).show();
});

$(document).on(&#39;click&#39;, function (e) {
  if ($(e.target).closest(&#39;#imglookupbox&#39;).length === 0) {
    $(&#39;#imglookupbox + .dropdown .dropdown-menu&#39;).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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input id=&quot;imglookupbox&quot; type=&quot;text&quot; value=&quot;&quot; placeholder=&quot;Search&quot; aria-label=&quot;Text input with dropdown button&quot;&gt;
&lt;div class=&quot;dropdown&quot;&gt;
  &lt;ul id=&quot;Imageoptions&quot; class=&quot;dropdown-menu&quot;&gt;
    &lt;li&gt;
      &lt;img onclick=&#39;Findimage()&#39; class=&#39;Img_Selection&#39; src=&quot;DataBaseSetup/Images/ImageName&quot; alt=&quot;no Image&quot;&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月20日 22:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定