简体   繁体   English

未捕获的语法错误:意外的令牌非法

[英]Uncaught syntax error:unexpected token illegal

I am trying to pass a string value to a basic javascript function and get the following error 我正在尝试将字符串值传递给基本的javascript函数并收到以下错误

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 这是函数这是javascript

function saveViewItemTester(prod) {
    alert(prod);
}

Help with resolving this little issue if possible 如果可能,帮助解决这个小问题

kind regards 亲切的问候

There are several issues: 有几个问题:

  • no types in javascript javascript中没有类型
  • value is reserved word, don't use it as variable name 值是保留字,请勿将其用作变量名
  • input tag isn't closed 输入标签未关闭
  • no need to "+" the variable 无需“ +”变量
  • use ; 采用 ; in javascript 在JavaScript中

Use this in between the head tags: 在head标签之间使用它:

<script type="text/javascript">
var test = "7dim-034/hallo/01:22";

function saveViewItemTester(prod) {
  alert(prod);
}
</script>

Use this between the body tags: 在body标签之间使用此标签:

<input type="button" value="submit" id="butSimulate" onClick="saveViewItemTester(test)" />

Javascript is a typeless language. Javascript是一种无类型的语言。 If you want a string, just assign it: 如果您想要一个字符串,只需分配它:

 value = "7dim-034/hallo/01:22";

Try this, in case value is C# code 如果值是C#代码,请尝试此操作

 <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" 对于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- C#代码

 public string value = "7dim-034/hallo/01:22";

Javascript Code- JavaScript代码-

<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. 也许您可以不使用“ +”传递值,也可以不使用String进行声明,只是var。 for example: 例如:

var value = "hola";

<input type="button" value="submit" id="butSimulate" onclick="saveViewItemTester(value)"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM