2017-03-06 50 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>

相關問題