简体   繁体   English

删除没有 html corespondent 的 Javascript 元素

[英]Removing Javascript element with no html corespondent

Might need some help with this code.可能需要一些有关此代码的帮助。 The purpouse is to click "check" and to display the image of wrong/right.目的是单击“检查”并显示错误/正确的图像。 Yet, the reset button doesn't reset the images spawned after the "Check" button.然而,重置按钮不会重置“检查”按钮后生成的图像。 So, if u press 10 times "check" the right/wrong images will appear 10 times in a row.因此,如果您按 10 次“检查”,正确/错误的图像将连续出现 10 次。 My questions would be?我的问题是?

  • Is it possible to DO NOT display more than one image after pressing "Check?"按“检查”后是否可以不显示多个图像?
  • Is it possible for the button "Reset" to un-display the images aswell as the text does? “重置”按钮是否可以像文本一样不显示图像?

I've posted the code below, but if any erors, here is the jfiddle https://jsfiddle.net/angelsmall13/xy5mkcz2/我已经发布了下面的代码,但如果有任何错误,这里是 jfiddle https://jsfiddle.net/angelsmall13/xy5mkcz2/

 var answers = { "q1": ["doesn't", "does not"], "q2": ["hasn't", "has not"], "q3": ["yes","no"] }; function markAnswers(){ $("input[type='text']").each(function(){ console.log($.inArray(this.value, answers[this.id])); if($.inArray(this.value, answers[this.id]) === -1){ $(this).parent().append("<img src=' https://i.ibb.co/c3hgCsQ/wrong.jpg' />"); } else { $(this).parent().append("<img src='https://i.ibb.co/w0ttgPg/check.jpg' />"); } }) } function myFunction() { document.getElementById("form").reset(); } $("form").on("submit", function(e){ e.preventDefault(); markAnswers(); $(".table").html(tableDefault); });
 #submit { background-color: darkgreen; /* Green */ border-radius: 50px; border: none; color: white; padding: 13px 50px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; }
 <form id="form"> <ol> <li>He <input id="q1" type="text" /> like football.</li> <li>He <input id="q2" type="text" /> got any money.</li> <li>He <input id="q3" type="text" /> got any money.</li> </ol> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem <input id="q1" type="text" /> Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining <input id="q1" type="text" /> essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <input type="submit" id="submit" value="Result" /> <input type="button" onclick="myFunction()" value="Reset form"> </form>

Give them a class and remove them给他们上课并删除他们

 var answers = { "q1": ["doesn't", "does not"], "q2": ["hasn't", "has not"], "q3": ["yes","no"] }; function markAnswers(){ $("input[type='text']").each(function(){ console.log($.inArray(this.value, answers[this.id])); if($.inArray(this.value, answers[this.id]) === -1){ $(this).after("<img class='grade' src=' https://i.ibb.co/c3hgCsQ/wrong.jpg' />"); } else { $(this).after("<img class='grade' src='https://i.ibb.co/w0ttgPg/check.jpg' />"); } }) } function myFunction() { document.getElementById("form").reset(); $(".grade").remove(); } $("form").on("submit", function(e){ e.preventDefault(); markAnswers(); // $(".table").html(tableDefault); });
 #submit { background-color: darkgreen; /* Green */ border-radius: 50px; border: none; color: white; padding: 13px 50px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="form"> <ol> <li>He <input id="q1" type="text" /> like football.</li> <li>He <input id="q2" type="text" /> got any money.</li> <li>He <input id="q3" type="text" /> got any money.</li> </ol> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem <input id="q1" type="text" /> Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining <input id="q1" type="text" /> essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <input type="submit" id="submit" value="Result" /> <input type="button" onclick="myFunction()" value="Reset form"> </form>

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

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