简体   繁体   中英

javascript quotation marks and apostrophes marks issue

its not working when i'm using multiple quotation and apostrophes in javascript. i'm trying to store javascirpt code in a variable and then want to echo in footer with the concern page. here im trying to create dynamic form which working perfectly but bcoz some error in quotation marks and apostrophes marks its not working. i tried this code in footer directly it working but i want it the view page to store it in FooterScript and then echo in the footer. so in footer script if u see whole code is insode the apostrophe. so inside one apostrophewe cant use another apostrophe we need to change it to qoutation mark. if im changing it to qoutation mark the problem araise here and id="'+i+'" if im changing apostrophes here to qoutation mark then its not working.i dont know how to achieve this

body with javascript code

<?php
$this->FooterScript=sprintf('
<script>
$(document).ready(function(){
var i=1;
$("#add").click(function(){
i++;
$("#dynamic_field").append("<tr id="row'+i+'"><td><select name="name[]" id="name[]" class="form-control name_list"><?php foreach($data_l as $data) { ?><option value="<?=$data->id;?>"><?=$data->name;?></option><?php } ?></select></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>");
});
$(document).on("click", ".btn_remove", function(){ var button_id = $(this).attr("id");
$("#row"+button_id+"").remove();
});
});
</script>');
?>

Footer

<?php echo $this->FooterScript; ?>

This is the problem

<?php foreach($data_l as $data) { ?><option value="<?=$data->id;?>"><?=$data->name;?></option><?php } ?>

PHP script inside a string, won't be executed. First process the above string and then attach it to the FooterScript

Edit Add \ before the quotation mark that you want to escape Ex: "\"" The second " will be escaped.

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