简体   繁体   English

在新标签页中打开后清除提交中的输入数据

[英]Clearing input data on submit after opening it in a new tab

Anyways, straight to the point. 无论如何,直截了当。 I am in need of some help on getting my form submit button to clear the input field as well as send the information that was typed. 在获取表单提交按钮以清除输入字段以及发送键入的信息时,我需要一些帮助。 Not very detailed, right? 不是很详细吧?

Here's an example. 这是一个例子。 Person types in field. 人员类型在字段中。 Person clicks submit. 人点击提交。 Submitted information is then opened in a new tab. 然后,在新选项卡中打开提交的信息。 The original tab with the input field is then refreshed to remove the text in the input field. 然后刷新带有输入字段的原始选项卡,以删除输入字段中的文本。

This is my current form. 这是我目前的表格。 I tried implemented javascript. 我尝试实现了JavaScript。 It worked for the removing the information on click, but didn't send the information to the handler. 它可以删除点击信息,但没有将信息发送给处理程序。 Can you tell me how to fix this? 你能告诉我如何解决这个问题吗?

<form class="form-inline" role="form" method="post" target="_blank" action="derp.php">

  <input id="banfix" type="text" name="player" class="form-control" placeholder="Player Username">
  <button type="submit" class="btn btn-default" onclick="document.getElementById('banfix').value='';return false;">Search</button>

</form>

I don't mind it being in php or javascript. 我不介意它是在php或javascript中。 I just need to know how to fix my problem. 我只需要知道如何解决我的问题。

Using jquery: 使用jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){
        //clear data  
        $('.form-inline').on('submit','.form-inline',function(){   
            var value = $("#banfix").val();//now you have the value
            $("input[type=text], textarea").val("");          
            });  
    });         
</script>

提交后可以重置您的表格

$(".myform")[0].reset();

Use onsubmit() , it will work when the user click the button or just press enter 使用onsubmit() ,当用户单击按钮或仅按Enter时它将起作用

<form class="form-inline" role="form" method="post" target="_blank" action="derp.php" onsubmit="document.getElementById('banfix').value='';">
    <input id="banfix" type="text" name="player" class="form-control" placeholder="Player Username">
    <button type="submit" class="btn btn-default">Search</button>
</form>

You can do the opening new window in derp.php file after submitting the form. 提交表单后,您可以在derp.php文件中打开新窗口。

<?php
        $banfix = $_POST['banfix'];
        $new_url = "YOUR_URL_HERE?fix=".$banfix;
        echo "<script type='text/javascript'>window.open('".$new_url."','_blank');</script>";
        http_redirect('YOUR_FORM_URL_HERE');
        die();
?>
<form class="form-inline" role="form" method="post" target="_blank" action="derp.php">
<input id="banfix" type="text" name="player" class="form-control" placeholder="Player Username">
<button type="submit" class="btn btn-default" onclick="setTimeout(function() {
document.getElementById('banfix').value='';return false;
}, 100);">Search</button>
</form>

I added a set timeout function to my onclick. 我向我的onclick添加了一个设置超时功能。 My problem was that the information being sent was being cleared before it was sent. 我的问题是正在发送的信息在发送之前已被清除。 So I set a 100 tick timeout so the info can be sent first. 因此,我将超时时间设置为100,以便可以先发送信息。 :3 :3

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

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