简体   繁体   English

Uncaught SyntaxError: Unexpected token } 原因不明

[英]Uncaught SyntaxError: Unexpected token } with unknow reason

I got an Unexpected token } , but I don't know why.我得到了一个Unexpected token } ,但我不知道为什么。

在此处输入图片说明

It is a table with some buttons on it.这是一张桌子,上面有一些按钮。 When the buttons is clicked, it will call the Javascript function.单击按钮时,它将调用Javascript函数。

Here is my code:这是我的代码:

<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();
            }
       }); 
    };
  }                 

The IDE don't have any syntax error. IDE没有任何语法错误。 Don't know why.不知道为什么。 Thanks.谢谢。

There error is in:有错误在:

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

Or rather:更确切地说:

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

That should be:那应该是:

<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>

Just enclosing the JavaScript with ' so double quotes are allowed so it would look like:只需用'将 JavaScript 括起来,这样就可以使用双引号,看起来像:

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

You Just forgot to Close your FOREACH Loop with "}" ;-)你只是忘了用“}”来关闭你的 FOREACH 循环;-)

just put就放

<?php } ?>

after the closing TR ... that should work.在结束 TR 之后......应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM