简体   繁体   English

尝试使用 javascript 对 html 表单上的数据做出反应

[英]Trying to react to data on an html form using javascript

I am not quite sure what is going wrong here.我不太确定这里出了什么问题。 I am not seeing values logged to the console upon selecting the "submit" event on the form.在选择表单上的“提交”事件时,我没有看到记录到控制台的值。

Here is my html:这是我的 html:

<div>
                <form class="submit-film">
                    <p class="email">Email</p>
                    <input class="field" id="email" placeholder="youremail@example.com">
                    <p class="project_link">Link to your project:</p>
                    <input class="field" id="link" placeholder="https://vimeo.com/myfilm">
                    <p class="pw">Password to access film:</p>
                    <input class="field" id="pw" placeholder="(password to access project)"> 
                    <p class="number">How many screeners do you want?</p>
                    <input class="field" id="screenerAmount" placeholder="8 screeners">
                    <p class="please">Please attach a questionaire if you have one preparred:</p>
                    <button class="select">Select file</button>
                    <p class="length">How many minutes is your project?</p>
                    <input class="field" id="minutes" placeholder="15 minutes">
                    <p class="questions">Do you have any additional comments or questions?</p>
                    <input class="field" id="comments" placeholder="(insert comments/questions here)"> 
                    <input type="submit" class="button_type" class="button_type:hover" value="submit"> 
                </form>
            </div>
    </div>
    <script src="wescreen.js"></script>
</body>
</html>

Here is the JavaScript:这是 JavaScript:

const form = document.querySelector('.submit-film'); 

form.addEventListener('.submit', e=> {
    e.preventDefault(); 
    console.log(form.email.value); 
    console.log(form.link.value); 
    console.log(form.pw.value);
    console.log(form.screenerAmount.value); 
    console.log(form.minutes.value); 
    console.log(form.comments.value); 
}); 

All your form elements are missing name attribute.您的所有表单元素都缺少name属性。

add them and try, something like this添加它们并尝试,像这样

<input class="field" name="email" id="email" placeholder="youremail@example.com">


console.log(form.email.value); 

Refer Here for a sample demo请参阅此处获取示例演示

Edit 1:编辑1:

Also some issue in this line此行中还有一些问题

form.addEventListener('.submit', e=> {

It should be submit应该是submit

form.addEventListener('submit', e=> {

Here is working fiddle这是工作小提琴

just changed the class to the id and used getelementbyid hope this helps刚刚将类更改为 id 并使用了 getelementbyid 希望这会有所帮助

   <div>
    <form id="submit-film">
        <p class="email">Email</p>
        <input class="field" id="email" placeholder="youremail@example.com">
        <p class="project_link">Link to your project:</p>
        <input class="field" id="link" placeholder="https://vimeo.com/myfilm">
        <p class="pw">Password to access film:</p>
        <input class="field" id="pw" placeholder="(password to access project)">
        <p class="number">How many screeners do you want?</p>
        <input class="field" id="screenerAmount" placeholder="8 screeners">
        <p class="please">Please attach a questionaire if you have one preparred:</p>
        <button class="select">Select file</button>
        <p class="length">How many minutes is your project?</p>
        <input class="field" id="minutes" placeholder="15 minutes">
        <p class="questions">Do you have any additional comments or questions?</p>
        <input class="field" id="comments" placeholder="(insert comments/questions here)">
        <input type="submit" class="button_type" class="button_type:hover" value="submit">
    </form>
</div>
</div>
<script>
    const form = document.getElementById('submit-film');
    console.log(form);
    form.addEventListener('click', e => {
        e.preventDefault();
        console.log(form.email.value);
        console.log(form.link.value);
        console.log(form.pw.value);
        console.log(form.screenerAmount.value);
        console.log(form.minutes.value);
        console.log(form.comments.value);
    }); </script>

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

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