我試圖將值連接到對象中另一個值(rollno
)的末尾。該值應該是每次增加1的數字(或字符串中的數字)。 (或者添加一個數字增加1的新屬性就會一樣好)。我嘗試了各種方法,下面是我設法得到的最接近的方法。該代碼只有一次然後休息。將變量連接到對象中的值
我該如何改變我的代碼來實現這一目標,或者我是否以這種錯誤的方式去做?
<button id='button'>add roll number</button>
student = {
name : "David Rayy",
sclass : "VI",
rollno : 12
};
function addRollno() {
var num
student.rollno = student.rollno + ' ' + (student.rollno +1);
console.log(student.rollno);
}
document.getElementById('button').addEventListener('click', addRollno, false);
使用'student.rollno ++'或'student.rollno + = 1'被一個每次遞增數(而不是'student.rollno = student.rollno +''+(student.rollno +1)') – pawel