如何在jQuery追加中添加引号一

时间:2019-06-22 18:49:10

标签: jquery html ajax

我想将onclick的格式从SendLowongan(000031)更改为SendLowongan('000031')

这是我的代码

var html = '<div id="buttonSend"><button type="button" class="btn btn-success" id="send" onclick="SendLowongan(' + getdata.IDLowongan + ')"><i class="fa fa-send"></i> Kirim Lowongan</button> <button type="button" class="btn btn-default" data-dismiss="modal">Tutup</button></div>';
        $("#footermodal").append(html);

1 个答案:

答案 0 :(得分:3)

您需要更改此部分:

...."SendLowongan(' + getdata.IDLowongan + ')"....

具有:

..."SendLowongan(\'' + getdata.IDLowongan.trim() + '\')"....
                 ^^                      ^^^^^^^    ^^

function SendLowongan(d) {
    console.log('--->' + d + '<----');
}

var getdata = {};
getdata["IDLowongan"] = " 000035";


var html = '<div id="buttonSend"><button type="button" class="btn btn-success" id="send" onclick="SendLowongan(\'' + getdata.IDLowongan.trim() + '\')"><i class="fa fa-send"></i> Kirim Lowongan</button> <button type="button" class="btn btn-default" data-dismiss="modal">Tutup</button></div>';
$("#footermodal").append(html);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" >


<div id="footermodal"></div>