jQuery 从特殊字符后的字符串中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4232462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value from a string after a special character
提问by Sullan
How do i trim and get the value after a special character from a hidden field The hidden field value is like this
我如何修剪并获取隐藏字段中特殊字符后的值隐藏字段值是这样的
Code
代码
<input type=-"hidden" val="/TEST/Name?3"
How i get the value after the "question mark" symbol in jquery??
我如何在 jquery 中的“问号”符号后获取值?
回答by Nick Craver
You can use .indexOf()
and .substr()
like this:
您可以使用.indexOf()
并.substr()
喜欢这样的:
var val = $("input").val();
var myString = val.substr(val.indexOf("?") + 1)
You can test it out here. If you're sureof the format and there's only one question mark, you can just do this:
你可以在这里测试一下。如果您确定格式并且只有一个问号,您可以这样做:
var myString = $("input").val().split("?").pop();
回答by Jan Han?i?
Assuming you have your hidden input in a jQuery object $myHidden
, you then use JavaScript (not jQuery) to get the part after ?
:
假设您在 jQuery 对象中有隐藏的输入$myHidden
,然后您使用 JavaScript(而不是 jQuery)来获取以下部分?
:
var myVal = $myHidden.val ();
var tmp = myVal.substr ( myVal.indexOf ( '?' ) + 1 ); // tmp now contains whatever is after ?
回答by Bart Kiers
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>
回答by Ganesh
//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);