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 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。