PHP生成的代码看起来很好,但在javascript中抛出错误

时间:2014-08-27 13:16:06

标签: javascript php jquery syntax-error

我有以下代码生成验证脚本的动态部分:

<script>
        $().ready(function() {

            $("#newtranslation").validate({
                    slanguage: "required",
                    tlangs: "required",
                    street: "required",
                    city: "required",
                    state: "required",
                    zip: "required",
                    docname: "required",
                    <?
                    $select_attachments = " SELECT  `doc_id`
                                            FROM    `li_appointments`.`li_trans_source_doc`
                                            Where   `trans_id` = $translation_id;";

                    $result = mysql_query($select_attachments);
                    while($row = mysql_fetch_array($result)){
                    $more_items = $more_items . $row['doc_id'] . 'pages: "required", ' . PHP_EOL;
                    $more_items = $more_items . $row['doc_id'] . 'words: "required", ' . PHP_EOL;
                    }
                    echo substr($more_items, 0, -2);
                    ?>
                },
                messages: {

                    slanguage: {
                        required: "Please enter a source language"
                    },
                    tlanguage: {
                        required: "Please enter a target language"
                    },
                    docname: {
                        required: "Please upload a document before submitting"
                    }
                },
                errorElement: "div",
                wrapper: "div",  // a wrapper around the error message
                 errorPlacement: function(error, element) {
                element.before(error);
                offset = element.offset();
                error.css('top', offset.top - element.outerHeight());
                }
            });
        });

输出以下内容:

$().ready(function() {

            $("#newtranslation").validate({
                    slanguage: "required",
                    tlangs: "required",
                    street: "required",
                    city: "required",
                    state: "required",
                    zip: "required",
                    docname: "required",
                    106pages: "required", 
 106words: "required", 
 107pages: "required", 
 107words: "required",                  },
                messages: {

                    slanguage: {
                        required: "Please enter a source language"
                    },
                    tlanguage: {
                        required: "Please enter a target language"
                    },
                    docname: {
                        required: "Please upload a document before submitting"
                    }
                },
                errorElement: "div",
                wrapper: "div",  // a wrapper around the error message
                 errorPlacement: function(error, element) {
                element.before(error);
                offset = element.offset();
                error.css('top', offset.top - element.outerHeight());
                }
            });
        });

但是我收到以下错误。我看不出有什么问题,为什么这不起作用?

  8                          state: "required",
  9                          zip: "required",
  10                          docname: "required",
  11                          106pages: "required", 
      =========^
      SyntaxError: missing : after property id
  12  106words: "required", 
  13  107pages: "required", 

14 107words:&#34; required&#34;,

2 个答案:

答案 0 :(得分:3)

您无法使用数字106pages启动属性/变量名称。它必须以字母,下划线或$。

开头

来自MDN

  

JavaScript标识符必须以字母,下划线(_)或美元符号($)开头;后续字符也可以是数字(0-9)。由于JavaScript区分大小写,因此字母包括字符“A”到“Z”(大写)和字符“a”到“z”(小写)。

     

从JavaScript 1.5开始,您可以在标识符中使用ISO 8859-1或Unicode字母,例如å和ü。您还可以将\ uXXXX Unicode转义序列用作标识符中的字符。

答案 1 :(得分:1)

我猜106pages: "required"应为"106pages": "required"。然后它应该在javascript中运行