简体   繁体   English

从html获取值并使用javascript更新DOM

[英]Take a value from html and update DOM with javascript

I have a basic webpage (not connected to a server) which takes information in a form and displays this information on the same page. 我有一个基本网页(未连接到服务器),该网页以某种形式获取信息并在同一页面上显示此信息。 One of the input options is a checkbox and I am struggling to display these selected values on the page. 输入选项之一是复选框,我正在努力在页面上显示这些选定的值。 I've been unable to decipher the other solutions I've come across. 我一直无法破译我遇到的其他解决方案。 There's a lot of text below but it's pretty basic. 下面有很多文字,但这很基础。

What do I need to display the results of the checkbox selections? 我需要显示复选框选择的结果吗? Are the results of a checkbox input saved in an array? 复选框输入的结果是否保存在数组中?

Any assistance will be of great use. 任何帮助将很有用。

 document.addEventListener('DOMContentLoaded', () => { const newItem = document.querySelector('#new-item'); newItem.addEventListener('submit', handleNewItemForm); }) const handleNewItemForm = function (event) { event.preventDefault(); const whiskyItem = createWhiskyListItem(event.target); const whiskyList = document.querySelector('#drunk-whiskies'); whiskyList.appendChild(whiskyItem); event.target.reset(); } const createWhiskyListItem = function (form) { const whiskyItem = document.createElement('li'); whiskyItem.classList.add('drunk-whiskies-item'); const name = document.createElement('h2'); name.textContent = form.name.value; whiskyItem.appendChild(name); const type = document.createElement('h3'); type.textContent = form.type.value; whiskyItem.appendChild(type); /**** This is the problem area. As above, I am looking to take the values from the form, make this into some text, before appending this to whiskyItem, which is then appended to whiskyList. */ return whiskyItem; } 
 <form id="new-item"> <div id="form-wrapper"> <div class="form-item"> <label for="name" class="primary">Name</label> <input type="text" id="name" required /> </div> <div class="form-item"> <label for="type" class="primary">Type</label> <input type="text" id="type" required /> </div> <div class="form-item"> <label for="flavour" class="primary">What did you taste?</label><br> <input type="checkbox" id="flavourProfile" value="boozy" name="boozy" checked> <label for="boozy">Booze</label> <input type="checkbox" id="flavourProfile" value="malty" name="malty"> <label for="malty">Malt</label> </div> </div> <input type="submit" value="save" /> </form> <h1>You've consumed...</h1> <div id="drunk-whiskies"></div> <ul></ul> 

You have Same ID's values for both checkboxes, which is incorrect. 两个复选框的ID值都相同,这是不正确的。 Change the ID's values to "flavourProfile1' and 'flavourProfile2' respectively. And try the following code. It will display the selected checkbox value in the <div id='drunk-whiskies'></div> . 分别将ID的值更改为“ flavorProfile1”和'flavorProfile2',然后尝试以下代码,它将在<div id='drunk-whiskies'></div>显示选定的复选框值。

    $("#flavourProfile1").click( function(){
   if( $(this).is(':checked') ) $("#drunk-whiskies").append('Booze');
    });

  $("#flavourProfile2").click( function(){
   if( $(this).is(':checked') ) $("#drunk-whiskies").append('Malt');
    });

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

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