duanbairan4235 2018-09-24 14:26
浏览 155
已采纳

如何使用onclick填充多个输入表单

I'm trying to create a live search with AJAX.

Then when I click the result I want the result to fill 5 input fields.

Here is what I have tried:

Here is my cari.php file:

while ($cari_karyawans = $cari_karyawan->fetch()) { ?>
   <div class="result" onclick="fill('nik','<?php echo $cari_karyawans['nik_k_ptayp']; ?>')">
   <div class="result" onclick="fill('nama','<?php echo $cari_karyawans['nama_k_ptayp']; ?>')">
   <div class="result" onclick="fill('lokasi','<?php echo $cari_karyawans['jabatan_k_ptayp']; ?>')">
   <div class="result" onclick="fill('divisi','<?php echo $cari_karyawans['divisi_k_ptayp']; ?>')">
   <div class="result" onclick="fill('jabatan','<?php echo $cari_karyawans['lokasi_k_ptayp']; ?>')">
   <a><small class="text-muted"><i><?php echo $cari_karyawans['nik_k_ptayp']; ?></i></small> / <small class="text-muted"><i><?php echo $cari_karyawans['nama_k_ptayp']; ?></i></small></a>
 </div></div></div></div></div>
<?php } ?>

Here is how I try to fill the result:

function fill(nik, nama, lokasi, divisi, jabatan) {
   $('#nik').val(nik);
   $('#nama').val(nama);
   $('#lokasi').val(lokasi);
   $('#divisi').val(divisi);
   $('#jabatan').val(jabatan);
   $('#display').hide();
}

Here is my input file:

<input type="text" class="form-control" id="nik">
<input type="text" class="form-control" id="nama">
<input type="text" class="form-control" id="lokasi">
<input type="text" class="form-control" id="divisi">
<input type="text" class="form-control" id="jabatan">
  • 写回答

1条回答 默认 最新

  • douzen1880 2018-09-24 14:30
    关注

    Your JS function fill should be :

    function fill(id, value) {
       $('#'+id).val(value);
    }
    

    Because you already sending the id as the first argument and the value as the second.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探