[英]how do i define this object in order to add event listeners to my form? unsure of what to do
我需要定義ShapeLink對象,以便允許我向檢查initForm函數的“提交”事件的表單中添加事件偵聽器。 但是,當我制作一個時,它說ShapeLink對象是未定義的,我在定義它時遇到了麻煩
<body>
<form>
<label for="name">Name:</label>
<input type="text" id="name">
<label for="shapeSelect">Shape:</label>
<select name="" id="shapeSelect">
<option value="">pick a shape</option>
<option value="square">Square</option>
<option value="circle">Circle</option>
</select>
<input type="submit" value="Submit">
</form>
<div id="outputText"></div>
<svg width=100 height=100></svg>
<script>
ShapeLink.initForm(document.querySelector("form"));
</script>
</body>
但我需要在我的JavaScript中添加它,我不確定在哪里以及為什么
let ShapeLink = (function(){
//code here!
return {
initForm: function(frm){
//code here!
}
}
})()
我所有的代碼都必須在我的JavaScript中。 我該怎么辦
我不知道您如何附加事件偵聽器,但是您的代碼似乎正在工作:
let ShapeLink = (function(){ //code here! return { initForm: function(frm){ //code here! frm.addEventListener('submit',function(){ alert('form submitting'); }) } } })() ShapeLink.initForm(document.querySelector("form"));
<form> <label for="name">Name:</label> <input type="text" id="name"> <label for="shapeSelect">Shape:</label> <select name="" id="shapeSelect"> <option value="">pick a shape</option> <option value="square">Square</option> <option value="circle">Circle</option> </select> <input type="submit" value="Submit"> </form> <div id="outputText"></div> <svg width=100 height=100></svg>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.