2017-03-06 49 views
1

JAVASCRIPT未捕获的SyntaxError:无效的正则表达式:缺少/,我缺少什么?

<script type="text/javascript"> 
function addnumber(element){ 
    document.getElementById(`mvar`).value = document.getElementById(`mvar`).value+element.value; 
} 
</script> 

HTML

<form action="" method="" name="vform"> 
    <input id=mvar type="text" value="" name="mvar"/><br/> 
    <input type="button" class="fbutton" name="1" value="1" id="1" onClick=addNumber(this);/> 
    <input type="button" class="fbutton" name="2" value="2" id="2" onClick=addNumber(this);/> 
    <input type="button" class="fbutton" name="3" value="3" id="3" onClick=addNumber(this);/> 

我缺少什么,或我思念的东西?我

+1

'的onClick = addNumber(本);'你需要添加身边'addNumber(本)报价;',现在的'/'的标签紧密理解为一部分onClick属性。 – Aaron

+1

什么与back ticks? –

+0

@Aaron引用是可选的 –

回答

0

请注意区分大小写的函数名称。另外,正如Pointy指出的那样(双关语意),onClick必须用引号括起来。

<script type="text/javascript"> 
    function addNumber(element){ 
     var myVar = document.getElementById('mvar'); 
     myVar.value = myVar.value + element.value; 
    } 
</script> 

<form action="" method="" name="vform"> 
<input id="mvar" type="text" value="" name="mvar"/><br/> 
<input type="button" class="fbutton" name="1" value="1" id="1" onClick="addNumber(this);" /> 
<input type="button" class="fbutton" name="2" value="2" id="2" onClick="addNumber(this);" /> 
<input type="button" class="fbutton" name="3" value="3" id="3" onClick="addNumber(this);" /> 
+1

虽然这并不能解释语法错误。 –

+0

@HristoYankov你通过在“onClick”属性值周围加引号来解决问题。 – Pointy

+0

函数名称大小写不匹配也是一个问题,并且会是他的代码将抛出的下一个异常。 –

0

试试下面的代码:没有必要把;在的onclick功能的结束,也是在函数名拼写错配。

function addNumber(element){ 
 
     document.getElementById('mvar').value = document.getElementById('mvar').value+element.value; 
 
    }
<form action="" method="" name="vform"> 
 
    <input id=mvar type="text" value="" name="mvar"/><br/> 
 
    <input type="button" class="fbutton" name="1" value="1" id="1" onClick=addNumber(this) /> 
 
    <input type="button" class="fbutton" name="2" value="2" id="2" onClick=addNumber(this) /> 
 
    <input type="button" class="fbutton" name="3" value="3" id="3" onClick=addNumber(this) /> 
 
</form>

相关问题