I am trying to pass a string value to a basic javascript function and get the following error
Uncaught syntax error:unexpected token illegal
This is the code behind
string value = "7dim-034/hallo/01:22"
<input type = 'button' value='submit' id='butSimulate' onClick='saveViewItemTester("+value+")'
and this is the function This is the javascript
function saveViewItemTester(prod) {
alert(prod);
}
Help with resolving this little issue if possible
kind regards
There are several issues:
Use this in between the head tags:
<script type="text/javascript">
var test = "7dim-034/hallo/01:22";
function saveViewItemTester(prod) {
alert(prod);
}
</script>
Use this between the body tags:
<input type="button" value="submit" id="butSimulate" onClick="saveViewItemTester(test)" />
Javascript is a typeless language. If you want a string, just assign it:
value = "7dim-034/hallo/01:22";
Try this, in case value is C# code
<input type = 'button' value='submit' id='butSimulate'
onClick='saveViewItemTester('<%=value%>')'/>
else use as
var value = "7dim-034/hallo/01:22";
<input type = 'button' value='submit' id='butSimulate'
onClick='saveViewItemTester('+ value +')'/>
只需在函数调用中将"
转换为'
,即可:
<input type = 'button' value='submit' id='butSimulate' onClick='saveViewItemTester('+value+')'/>
for C#, string value = "7dim-034/hallo/01:22"
<input type = 'button' value='submit' id='butSimulate'
onClick='saveViewItemTester("<%=value%>")'/>
这应该工作
<input type="button" value="submit" id="butSimulate" onclick="saveViewItemTester('7dim-034/hallo/01:22')" />
Re-factor your code as follows-
C# code-
public string value = "7dim-034/hallo/01:22";
Javascript Code-
<input type = 'button' value='submit' id='butSimulate' onclick='saveViewItemTester("<%=value%>")'/>
function saveViewItemTester(prod) {
alert(prod);
}
maybe you can pass the value without "+" and declaring without String, just var. for example:
var value = "hola";
<input type="button" value="submit" id="butSimulate" onclick="saveViewItemTester(value)"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.