代码之家  ›  专栏  ›  技术社区  ›  jainilvachhani

ReferenceError:分配JavaScript中的左侧无效

  •  0
  • jainilvachhani  · 技术社区  · 8 年前

    <form action="/" method="POST" name="search-theme-form" id="search-theme-form" >
    User <input type="text" name="user" />
    Term<input type="text" name="term" />
    Subject<input type="text" name="subject" />
    Grade<input type="text" name="grade" />
    Marks<input type="text" name="marks" />
    <script>
    var params = (new URL(document.location)).searchParams;
    var studentId = params.get("stdid");
    var studentId = params.get("stdid");
    var x = "app/students/"  + studentId;
    document.search-theme-form.action = x;
    </script>
        <input id="submitButton"  type="submit"value="Submit" />
    </form>
    
    2 回复  |  直到 8 年前
        1
  •  3
  •   Nikhil Aggarwal    8 年前

    你不能使用 dot (.) hyphen 在他们里面。使用 bracket notation [] 以访问属性。

    document["search-theme-form"].action = x;
    
        2
  •  2
  •   Ankit Agarwal    8 年前

    你需要使用 document.querySelector('[name="search-theme-form"]') console 检查错误并尝试解决该问题。问题是您没有选择 form 元素如此使用 将选择 形式 元素,然后添加 action 去吧。因此 document.search-theme-form.action 是 undefined 在您需要使用的左侧 document['search-theme-form'].action

    var params = (new URL(document.location)).searchParams;
    var studentId = params.get("stdid");
    var studentId = params.get("stdid");
    var x = "app/students/"  + studentId;
    document.querySelector('[name="search-theme-form"]').action = x;
    <form action="/" method="POST" name="search-theme-form" id="search-theme-form" >
    User <input type="text" name="user" />
    Term<input type="text" name="term" />
    Subject<input type="text" name="subject" />
    Grade<input type="text" name="grade" />
    Marks<input type="text" name="marks" />