Get value from a string after a special character

后端 未结 4 689
生来不讨喜
生来不讨喜 2021-01-30 10:26

How do i trim and get the value after a special character from a hidden field The hidden field value is like this

Code



        
相关标签:
4条回答
  • 2021-01-30 10:49

    //var val = $("#FieldId").val()
    //Get Value of hidden field by val() jquery function I'm using example string.
    var val = "String to find after - DEMO"
    var foundString = val.substr(val.indexOf(' - ')+3,)
    console.log(foundString);
    Assuming you need to find DEMO string after - by above code you can able to access DEMO string substr will return the string from whaterver the value indexOf return till the end of string it will return everything.

    0 讨论(0)
  • 2021-01-30 10:54

    You can use .indexOf() and .substr() like this:

    var val = $("input").val();
    var myString = val.substr(val.indexOf("?") + 1)
    

    You can test it out here. If you're sure of the format and there's only one question mark, you can just do this:

    var myString = $("input").val().split("?").pop();
    
    0 讨论(0)
  • 2021-01-30 11:05

    Here's a way:

    <html>
        <head>
            <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(document).ready(function(){
                    var value = $('input[type="hidden"]')[0].value;
                    alert(value.split(/\?/)[1]);
                });
            </script>
        </head>
        <body>
            <input type="hidden" value="/TEST/Name?3" />
        </body>
    </html>
    
    0 讨论(0)
  • 2021-01-30 11:07

    Assuming you have your hidden input in a jQuery object $myHidden, you then use JavaScript (not jQuery) to get the part after ?:

    var myVal = $myHidden.val ();
    var tmp = myVal.substr ( myVal.indexOf ( '?' ) + 1 ); // tmp now contains whatever is after ?
    
    0 讨论(0)
提交回复
热议问题