简体   繁体   English

提交后如何清除文字栏位值

[英]How do I clear the text field value after submitting

Hey guys does someone of you know how to delete the text field after I click submit? 大家好,我单击提交后知道如何删除文本字段吗?

I have a button "senden" and after I click on this button the value of the text fields above should be deleted and the check box must be unchecked again. 我有一个“ senden”按钮,单击此按钮后,上面的文本字段的值应删除,并且必须再次取消选中该复选框。

I tried some tutorials but it didn' work. 我尝试了一些教程,但是没有用。

Here is my code, if someone wants to help me 这是我的代码,如果有人想帮助我

Try again with jQuery: 使用jQuery再试一次:

$('#submit').click(function() {
     //Other code here

     //And end your function with this:
     $("#name").val("");
     $("#ort").val("");
     $("#datum").val("");
     $("input [name='Favorit']").prop("checked", false);

});

EDIT: 编辑:

In order your checkbox to work, set an id , for example: 为了使您的复选框正常工作,请设置一个id ,例如:

<input type="checkbox" name="Favorit" id="Favorit" value="Favorit">Favorit </input>

Afterwards target the checkbox with: 然后,将复选框定位为:

 $('#submit').click(function() {
 //Other code here

 //And end your function with this (after you gave the checkbox `id`):
 $("#name").val("");
 $("#ort").val("");
 $("#datum").val("");
 $("#Favorit").prop("checked", false);

 });

Kindly, run the snippet in this answer and see if it meets your requirement. 请运行此答案中的代码片段,看看它是否满足您的要求。 You will find the changes I have done to your code delimited by CHANGES comment in the HTML and JavaScript code. 在HTML和JavaScript代码中,您会发现我对您的代码所做的更改(由CHANGES注释分隔)。

I hope it does and here are my observations: 希望如此,这是我的观察:

  1. Every input that you you want to be submitted (in a standard form submission) must be inside the form ie between the opening <form> and the closing </form> . 您要提交的每个输入(以标准表单提交)必须在form内部,即在开始<form>和结束</form> Thus, I took the liberty of moving the checkbox inside the form. 因此,我自由地将复选框移到了表单内。 Kindly ensure this does not break your existing code. 请确保这不会破坏您现有的代码。
  2. I found client-side validation in your JavaScript code and added the input-clearing code at the end of the success case. 我在您的JavaScript代码中找到了客户端验证,并在成功案例的末尾添加了输入清除代码。 To make the selection of input elements general (and currently limited to textbox and checkbox), I have added the form ID and a general input selector with type, as follows: 为了使输入元素的选择更为通用(目前仅限于文本框和复选框),我添加了表单ID和具有类型的通用输入选择器,如下所示:

    // clear the input fields if the form has been processed $('#send input[type="text"]').val(''); $('#send input[type="checkbox"]').prop('checked', false);

 window.onload = function() { var allArtists = []; $('#submit').click(function() { var $rowTemplate = $('<tr><td data-id="id"></td><td data-id="name"></td><td data-id="geburtsort"></td><td data-id="geburtsdatum"></td><td data-id="favorite"></td></tr>'); var artistName = $("#name").val(); var ort = $("#ort").val(); var datum = $("#datum").val(); var favourite = $("[name=Favorit]").is(':checked'); if(artistName!= "" && ort != "" && datum != ""){ $('.fehlermeldung').hide(); allArtists.push([artistName, ort, datum]); var rowId = allArtists.length; $rowTemplate.find('[data-id=id]').text(rowId); $rowTemplate.find('[data-id=name]').text(artistName); $rowTemplate.find('[data-id=geburtsort]').text(ort); $rowTemplate.find('[data-id=geburtsdatum]').text(datum); var checked = favourite ? "checked" : ""; $rowTemplate.find('[data-id=favorite]').html('<div class="chkText">'+favourite+'</div>').append($('<input type="checkbox" id="fave" ' + checked + '>')); $("#table tbody").append($rowTemplate); // CHANGES START // clear the input fields if the form has been processed $('#send input[type="text"]').val(''); $('#send input[type="checkbox"]').prop('checked', false); // CHANGES END }else{ $('.fehlermeldung').show(); } }); $("#table").on('change','input[type=checkbox]',function(){ $(this).prev('div').text($(this).is(":checked")); }); }; function myFunction() { var input, filter, table, tr, td, i; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); table = document.getElementById("table"); tr = table.getElementsByTagName("tr"); // Loop through all table rows, and hide those who don't match the search query for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[1]; if (td) { if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } 
 .chkText{ float:left; } .fehlermeldung{ display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Artist</title> </head> <script src="js/jquery-3.1.1.min.js"></script> <script src="js/main.js"></script> <link rel="stylesheet" href="css/style.css"> <body> <div id="layout"> <h1>Künstler hinzufügen</h1> <form id="send"> <label>Add artists</label> <br> <input id="name" type="text" placeholder="Name des Künstlers" /> <br> <label>Ort</label> <br> <input id="ort" type="text" placeholder="Woher stammt der Künstler" /> <br> <label>Geburtsdatum</label> <br> <input id="datum" type="text" placeholder="Wann ist der Künstler geboren?" /> <br> <!-- CHANGES START - moving the checkbox inside form --> <input type="checkbox" name="Favorit" value="Favorit">Favorit </input> <!-- CHANGES END --> </form> <!-- CHANGES START - removing checkbox from outside the form --> <!-- <p> <input type="checkbox" name="Favorit" value="Favorit">Favorit </input> <p> --> <!-- CHANGES END --> <input type="button" id="submit" name="senden" value="Senden"> <div class="fehlermeldung"> <label id="fehler" style="color: #962d2d"> Bitte füllen Sie alle Zeilen aus!</label> </div> <!-- CHANGES START --> <br /> <!-- CHANGES END --> <input type="text" id="myInput" onkeyup="myFunction()" placeholder=" Search for names.."> <table id="table"> <tbody> <tr> <th>ID</th> <th>Name</th> <th>Geburtsort</th> <th>Geburtsdatum</th> <div> <th>Favorit</th> </div> </tr> </tbody> </table> </div> </body> </html> 

Try this one: 试试这个:

 $('#submit').click(function() { //your codes $("form#send").find('input:text').val(''); // I reccomend to move all input from the same group inside <form> tag. //in this case, <button id=submit> and <input type=checkbox> should be //moved within form#send //$("form#send").find('input:checkbox').prop('checked',false); //or $("input:checkbox[name='Favorit']").prop('checked', false); //set the focus $("#send").find('input:text')[0].focus(); }); 
 <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script> <form id="send"> <label>Add artists</label> <br> <input id="name" type="text" placeholder="Name des Künstlers" /> <br> <label>Ort</label> <br> <input id="ort" type="text" placeholder="Woher stammt der Künstler" /> <br> <label>Geburtsdatum</label> <br> <input id="datum" type="text" placeholder="Wann ist der Künstler geboren?" /> <br> <!--better to keep this inside form--> <p> <input type="checkbox" name="Favorit" value="Favorit">Favorit</input> </p> <input type="button" id="submit" name="senden" value="Senden"> </form> 

  window.onload = function() { var allArtists = []; $('#submit').click(function() { var $rowTemplate = $('<tr><td data-id="id"></td><td data-id="name"></td><td data-id="geburtsort"></td><td data-id="geburtsdatum"></td><td data-id="favorite"></td></tr>'); var artistName = $("#name").val(); var ort = $("#ort").val(); var datum = $("#datum").val(); var favourite = $("[name=Favorit]").is(':checked'); if(artistName!= "" && ort != "" && datum != ""){ $('.fehlermeldung').hide(); allArtists.push([artistName, ort, datum]); var rowId = allArtists.length; $rowTemplate.find('[data-id=id]').text(rowId); $rowTemplate.find('[data-id=name]').text(artistName); $rowTemplate.find('[data-id=geburtsort]').text(ort); $rowTemplate.find('[data-id=geburtsdatum]').text(datum); var checked = favourite ? "checked" : ""; $rowTemplate.find('[data-id=favorite]').html('<div class="chkText">'+favourite+'</div>').append($('<input type="checkbox" id="fave" ' + checked + '>')); $("#table tbody").append($rowTemplate); //And end your function with this (after you gave the checkbox `id`): $("#name").val(""); $("#ort").val(""); $("#datum").val(""); $("#Favorit").prop("checked", false); }else{ $('.fehlermeldung').show(); } }); $("#table").on('change','input[type=checkbox]',function(){ $(this).prev('div').text($(this).is(":checked")); }); }; function myFunction() { var input, filter, table, tr, td, i; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); table = document.getElementById("table"); tr = table.getElementsByTagName("tr"); // Loop through all table rows, and hide those who don't match the search query for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[1]; if (td) { if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } 
  .chkText{ float:left; } .fehlermeldung{ display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="layout"> <h1>Künstler hinzufügen</h1> <form id="send"> <label>Add artists</label> <br> <input id="name" type="text" placeholder="Name des Künstlers" /> <br> <label>Ort</label> <br> <input id="ort" type="text" placeholder="Woher stammt der Künstler" /> <br> <label>Geburtsdatum</label> <br> <input id="datum" type="text" placeholder="Wann ist der Künstler geboren?" /> <br> </form> <p> <input type="checkbox" name="Favorit" value="Favorit">Favorit </input> <p> <input type="button" id="submit" name="senden" value="Senden"> <div class="fehlermeldung"> <label id="fehler" style="color: #962d2d"> Bitte füllen Sie alle Zeilen aus!</label> </div> <input type="text" id="myInput" onkeyup="myFunction()" placeholder=" Search for names.."> <table id="table"> <tbody> <tr> <th>ID</th> <th>Name</th> <th>Geburtsort</th> <th>Geburtsdatum</th> <div> <th>Favorit</th> </div> </tr> </tbody> </table> </div> 

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

相关问题 提交表单后如何清除之前的文本字段值而不刷新整个页面? - How do I clear the previous text field value after submitting the form with out refreshing the entire page? 提交后如何重新加载、清除或重定向 PHPmailer 表单? - How do I reload, clear or redirect a PHPmailer form after submitting? 提交数据库后如何清除输入字段 - How to clear input field after submitting to database 明文字段不清空输入值 - Clear text field do not empty the input value 输入值后如何清除输入字段? - How can I clear a input field after entering a value? 输入错误后如何清除输入字段 - How do I clear the input field after an incorrect entry 如何在一种形式(提交后)中将值(在文本字段中)显示为另一种形式(两者都在同一页面上) - how to display a value(in text field) from one form(after submitting it) to another form (both are on the same page) 提交表单后如何清除字段并在页面重新加载后显示表单值 - How to clear a field after submitting a form and display the form values after the page reloads 提交表单并在一个字段中搜索其他 3 个按钮后,如何在浏览器中保存或保留信息/状态 - How do I save or keep information/state in the browser after submitting form and searching with one field for 3 other buttons 如何清除文本字段? - How to clear a text field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM