Escape single quotes in jQuery or JavaScript

后端 未结 4 1614
野性不改
野性不改 2020-12-07 02:20

I want to display the following text for the tag. How do I escape the single quotes for the following?

$(\"#spn_err\").text($(\'#tx         


        
相关标签:
4条回答
  • 2020-12-07 02:39

    It is close. Use the .val() method:

    $('#spn_err').text($('#txt1').val() + ' is not valid');
    
    0 讨论(0)
  • 2020-12-07 02:50
    $("#spn_err").text("'" + $('#txt1').attr('value') + "' is not valid");
    
    0 讨论(0)
  • 2020-12-07 02:58

    Like this:

    $("#spn_err").text("'" + $('#txt1').val() + "' is not valid");
    

    Inside double quotes, single quotes are normal characters and vice versa. Otherwise you can escape them by prepending a backslash: "\"" or '\''.

    0 讨论(0)
  • 2020-12-07 02:58
    $("#spn_err").text('\'' + $("#txt1").attr("value") + '\' is not valid');
    
    0 讨论(0)
提交回复
热议问题