using a javascript variable
I'm having trouble with this script recognizing my variable as a variable. The important part of the script is as follows:
var content = 'imagereims';
var ms = content.substring(5);
$.get("../msimages/image.php", {ms: 'ms', pid: '<?php echo "$pid" ?>'}
);
I want the script to recognize the variable ms
as reims
, but when the page displays it doesn't recognize the content of the variable. It just repeats ms
. I've tried writing the variable without single quotes and with double quotes. I get the same result.
Any sugg开发者_StackOverflow社区estions. Thanks
You'll want to leave off the quotes on 'ms'. Try this:
var content = 'imagesreims'
var ms = content.substring(4);
$.get("../msimages/image.php", {ms: ms, pid: '<?php echo "$pid" ?>'});
'ms' is saying use this string literal as the value.
You'll also want a different substring. Try content.substring(6)
. content.substring(4)
will yield 'esreims'.
As mentioned in another answer, you missed the );
at the end, which I've included in my answer.
Here's what you would do:
var content = 'imagesreims'
var ms = content.substring(6);
$.get("../msimages/image.php", {ms: ms, pid: '<?php echo "$pid" ?>'}
Note two things:
- use
ms
as the variable, rather than the literal 'ms' - correct index 4 to 6, if you want
reims
Current your line:
var ms = content.substring(4);
is assigning ms
the value esreims
. See documentation here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring
All of these answers have pointed out errors in the code. You'll want to draw from them all.
精彩评论