2016-09-21 121 views
1

我得到了Unexpected token },但我不知道爲什麼。未捕獲的SyntaxError:意外的令牌}帶有未知原因

enter image description here

它與它的一些按鈕的表格。當按鈕被點擊時,它會調用Javascript函數。

這裏是我的代碼:

<tbody> 
<?php $i = 0; foreach ($query as $row){?> 
<tr> 
    <td><?php echo ++$i; ?></td> 
    <td><?php echo $row['address'] . ", " . $row['city'] . ", " . $row['state'] . ", " . $row['country']; ?></td> 
    <td> 
     <button id ="delete" class = "delete" type="button" value=<?php echo "\"" . $row['id'] . "\""; ?> onClick="deleteAddress(this.value)">delete</button> 
    </td> 
    <td> 
    //Print this <button id = "edit" class = "edit" type="button" value="7" onClick = "editAddress(this.value, "23", "323", "323", "iij")" >Edit</button> 
     <button id = "edit" class = "edit" type="button" value=<?php echo "\"" . $row['id'] . "\""; ?> onClick = <?php echo "\"editAddress(this.value" . ", \"" . trim($row['address']) . "\", \"" . $row['city'] . "\", \"" . $row['state'] . "\", \"" . $row['country'] . "\")\""; ?> >Edit</button></td> 
    </tr> 
</tbody> 

function editAddress(id, address, city, state, country) 
{ 
    document.getElementById("address_field").value = address; 
    document.getElementById("city_field").value = city; 
    document.getElementById("state_field").value = state; 
    document.getElementById("country_field").value = country; 
    document.getElementById("add_new").innerHTML = 'Update Address'; 
    document.getElementById("add_new").value = id; 
    document.getElementById("add_new").onclick = function() { 
     var id = document.getElementById("add_new").value; 
     var address = document.getElementById("address_field").value; 
     var city = document.getElementById("city_field").value; 
     var state = document.getElementById("state_field").value; 
     var country = document.getElementById("country_field").value; 

     jQuery.ajax({ 
      type: "POST", 
      url: "../wp-content/plugins/add_address_list/insert_address.php", 
      data: {functionName : "updateAddress", id : id, address : address, city : city, state : state, country : country}, 
      success: function(msg){ 
       window.location.reload(); 
      } 
     }); 
    }; 
    }     

IDE沒有任何語法錯誤。不知道爲什麼。 謝謝。

+1

錯誤來自PHP或JavaScript,它不是很清楚嗎?你有幾個語法錯誤,例如'onClick =「editAddress(this.value,」23「,」323「,」323「,」iij「)」'不會工作,因爲你在同時您爲該屬性使用雙引號。 – adeneo

+0

Thx。將雙引號更改爲單引號,然後它現在正在工作。謝謝。 – Capslock10

回答

1

有錯誤是:

onClick = "editAddress(this.value, "23", "323", "323", "iij")" 

或者說:

onClick = <?php echo "\"editAddress(this.value" . ", \"" . trim($row['address']) . "\", \"" . $row['city'] . "\", \"" . $row['state'] . "\", \"" . $row['country'] . "\")'"; ?> >Edit</button> 

這應該是:

<button id = "edit" class = "edit" type="button" value=<?php echo "\"" . $row['id'] . "\""; ?> onClick ='<?php echo "editAddress(this.value" . ", \"" . trim($row['address']) . "\", \"" . $row['city'] . "\", \"" . $row['state'] . "\", \"" . $row['country'] . "\")"; ?> >Edit</button></td> 

只是封閉與'所以雙引號的JavaScript被允許,因此看起來像:

onClick = 'editAddress(this.value, "23", "323", "323", "iij")' 
+0

更改雙引號單引號,它正在工作。謝謝! – Capslock10

0

您剛纔忘了與閉上你的foreach循環 「}」 ;-)

只是把

<?php } ?> 

收盤後TR ...這應該工作。

+0

我忘了複製到這裏。 LOL – Capslock10

+0

哦..哈哈..好吧.. – victor

相關問題