jquery escape square brackets to select element

前端 未结 6 1353
余生分开走
余生分开走 2021-01-03 04:55

Consider a input element


Here the input field is dynamically generat

6条回答
  •  别那么骄傲
    2021-01-03 05:25

    The following should work:

    alert($('#meta\[152\]\[value\]').val());
    

    or

    var id = "#meta\[152\]\[value\]";
    alert($(id).val());
    

    Working Example

    Conversion Function:

    function ConvertValue(id)
    {
        var test = id.replace(/[[]/g,'\\\\[');
        return "#" + test.replace(/]/g,'\\\\]'); 
    }
    

    Conversion Example

提交回复
热议问题