忽略 Jquery 函数单引号

Jquery function single quotes ignored

本文关键字:单引号 函数 Jquery 忽略      更新时间:2023-09-26

>我有这个函数

    function getTextfieldonly(fieldName, id) {
        var option = jQuery('#'+fieldName+'_textfield').val();
        if (option != "") {
        $('.'+fieldName+'_ul').append('<li class="'+fieldName+'_item">'+option+'<span onclick="removeSelected($(this),'+fieldName+')" class="reLef buttonleistungspktr '+fieldName+'_button2"></span></li>');
        $('#'+fieldName+'_list').prepend('<option selected="selected" value="'+option+'">'+option+'</option>');
        }else {return false}
    };

在第 4 行中,我附加了一个带有跨度的 Li

.append('<li class="'+fieldName+'_item">'+option+'<span onclick="removeSelected($(this),'+fieldName+')" class="reLef buttonleistungspktr '+fieldName+'_button2"></span></li>');

如果字段名称参数例如为"foo">

我希望附加范围内的点击看起来像

onclick="removeSelected($(this),'foo')"

但它的渲染方式是

onclick="removeSelected($(this),foo)"

那么单引号去哪儿了呢?我需要他们在那里,我不知道怎么做。

有人有想法吗? 提前感谢

更改此内容

<span onclick="removeSelected($(this),'+fieldName+')"

<span onclick="removeSelected($(this),'''+fieldName+''')"

您需要转义单引号