douke6881 2017-09-08 06:12
浏览 59
已采纳

从ajax响应中选择文本,并在用户单击项目时显示在输入字段中

user type london record fetch from data base and show below the input field

<input type="text" id="inputSuccess" name="name" onkeyup="autosearch(this.value)" > //user type london 

when user click on london than london add in input field but it cannot set in input field what jquery script require to select the value from p tag and show in input field

<script>
function autosearch(name){
   //get data from database
   $.ajax({
       url:"ajax.php",
       type:'post',
       data: {name: name},
       success:function(result){
           $('.result').html(result);
       }
   });
}
</script>

ajax.php

<div style="border: 1px solid #ccc;width:100%;" >
   <a href="javascript:void(0)">
</div>
<div>
  <p><?php echo $data['cityName']; ?> </p> //london show here 
</div>
  </a>
</div>
  • 写回答

2条回答 默认 最新

  • dtnpzghys01643322 2017-09-08 06:46
    关注

    If I got your question then Here is an example to let you clear how to add text in the input. Please checkout below:

    HTML:

    <input type="text" id="inputSuccess" name="name" onkeyup="autosearch(this.value)" >
    
    <a href="javascript:void(0)" data-val="textToShow" onclick="addTextInInput($(this).data('val'))">
      <div>
        <p>asd </p> 
      </div>
    </a>
    

    JS:

    function addTextInInput(txt){
     //get data from database
       $("#inputSuccess").val(txt)
     }
    

    Replace the textToShow in data-val with your PHP code and try it. Also, find working example here https://jsbin.com/witilohinu/1/edit?html,js,console,output

    Please give your feedback in the comment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计