简体   繁体   中英

How to prepend a Hashtag to an attribute of a data-target when adding a new target .Using pure JS

I am new to this, so thank you for you help. From my Sql database result rows I use a php variable to add the row id to a buttons data-target. The button opens my modal in a bootstrap card. The row id gives it a unique number. It works well below.

<a class="my-btn" data-toggle="modal"  data-backdrop="static" data-keyboard="false" data-target="#modal<?php echo $rowid;?>" >MyButton</a>

Problem: I have moved to Dynamic creation of records. The data now comes in a json array via a GET. So to process the data I use change JS Attribute

$('.card').find('.my-btn').attr('data-target',record.rowid); 

The above applies it, but the button does not work because I Have not prepended the hash tag. is there a way to add the hash like below example.

$('.card').find('.my-btn').attr('data-target',#record.rowid); 

My full script is below.

<script>
$(document).ready(function(){

   $.ajax({
       url: 'scripts/sql.php',
       type: 'get',
       dataType: 'JSON',
       success: function(result){
       data =(result);


   $.each(data, function( i, record ) {
   if(i == 0) {

     $('.card').find('.firstname').text(record.firstname);
     $('.card').find('.surname').text(record.surname);
     $('.card').find('.my-btn').attr('data-target',record.recordid);
     
   
   } else {
   
     var recordDetailCloned = $('.card').first().clone();
     recordDetailCloned.find('.firstname').text(record.firstname);
     recordDetailCloned.find('.surname').text(record.surname);
     recordDetailCloned.find('.my-btn').attr('data-target',record.recordid);
  

      $('.card-container').append(recordDetailCloned);
      
     

   }
     
 });
 
 }
});

});
</script>

You may just be looking to prefix your variable with a string.

This can be done one of two ways:

`#${record.recordid}`

or

'#' + record.recordid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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