简体   繁体   English

将实时搜索的点击结果放入文本框

[英]Put click results of Live Search into a textbox

I'm performing a live search with my script below and it's working perfectly. 我正在使用下面的脚本执行实时搜索,并且运行良好。 The issue is in Chrome when the user clicks on the desired results it gets inserted into a textbox, but it's failing in Firefox. 当用户单击所需的结果并将其插入文本框时,Chrome中出现问题,但在Firefox中失败。

Secondly, instead of inserting only the text into the textbox, it inserts html tags as well. 其次,它不仅插入文本到文本框中,而且还插入html标签。 What I want is the text alone. 我想要的只是文字。

 $(function() {

     $(".search_tab").keyup(function() {
         var searchid = $(this).val();
         var dataString = 'color=' + searchid;
         if (searchid != '') {
             $.ajax({
                 type: "POST",
                 url: "../search.php",
                 data: dataString,
                 cache: false,
                 success: function(html) {

                     $("#result").html(html).show();
                 }
             });
         }
         return false;
     });

     $("#result").on("click", function(e) {
         var $clicked = $(e.target);
         if (!$clicked.hasClass("search")) {
             $('input.search_tab').val(event.target.innerHTML);
             jQuery("#result").fadeOut();
         }
     });

     $('.search_tab').click(function() {
         jQuery("#result").fadeIn();
     });

 });

I disagree with the other answers here. 我不同意这里的其他答案。 You can't call event.target because it will tell you that event is undefined, as your event parameter is e , not event . 您无法调用event.target因为它会告诉您event未定义,因为您的事件参数是e ,而不是event

Chrome and IE are okay with this because they will provide event if it's not defined, though other browsers (like Firefox) will not. Chrome和IE可以这样做,因为如果未定义event它们将提供event ,而其他浏览器(如Firefox)则不会。


You already save the clicked element here: 您已经在此处保存了单击的元素:

var $clicked = $(e.target);

So why store this, only to suddenly switch to event.target.innerHTML ? 那么,为什么要存储它,而只是突然切换到event.target.innerHTML

Do this instead: 改为这样做:

 $("#result").on("click", function(e) {
     var $clicked = $(e.target);
     if (!$clicked.hasClass("search")) {
         $('input.search_tab').val($clicked.text());
         $(this).fadeOut();
     }
 });

I've fixed the Firefox issue by simply using the $clicked variable that you were already using. 我已经解决了Firefox问题,只需使用您已经在使用的$clicked变量即可。 To grab text only (and not the HTML), I've used jQuery's .text() . 为了仅捕获文本(而不捕获HTML),我使用了jQuery的.text()

Working example: https://jsfiddle.net/yuztjbva/4/ 工作示例: https : //jsfiddle.net/yuztjbva/4/

您可以使用jQuery.text方法

   $('input.search_tab').val($clicked.text());

You must change this: 您必须更改此:

success: function(html) {

                 $("#result").html(html).show();
             }

to this: 对此:

            success: function(html) {

                 $("#result").html(html.d).show();
             }

And if you want only text: 如果只想输入文字:

$("#result").html($(html.d).text()).show();

像这样的textContent而不是innerHTML

$('input.search_tab').val(e.target.textContent);

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

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