2017-09-25 49 views
0

我在此代碼獲取上述錯誤:對象數組中:意外的字符串

var inputs = { 
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID), 
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID), 
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID), 
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID), 
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID), 
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID) 
} 

這看起來很有效的給我。我想.append()變量對一個div,我不想創建一堆不同的變量來做到這一點。

+2

對象語法'{鍵:值}' – Tushar

+2

您使用數組文本方括號('[]')。大括號表示期望鍵值對的對象。 –

+0

對象需要有鍵和值 –

回答

6

inputs需要是Array。使用[]代替{}

var collegeID = ""; 
 
var inputs = [ 
 
    $("<input>").attr({ 
 
    "type": "hidden", 
 
    "name": "collegeID" 
 
    }).val(collegeID), 
 
    $("<input>").attr({ 
 
    "type": "hidden", 
 
    "name": "collegeID" 
 
    }).val(collegeID), 
 
    $("<input>").attr({ 
 
    "type": "hidden", 
 
    "name": "collegeID" 
 
    }).val(collegeID), 
 
    $("<input>").attr({ 
 
    "type": "hidden", 
 
    "name": "collegeID" 
 
    }).val(collegeID), 
 
    $("<input>").attr({ 
 
    "type": "hidden", 
 
    "name": "collegeID" 
 
    }).val(collegeID), 
 
    $("<input>").attr({ 
 
    "type": "hidden", 
 
    "name": "collegeID" 
 
    }).val(collegeID) 
 
] 
 

 
console.log(JSON.stringify(inputs));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>