简体   繁体   English

如何在没有按钮的情况下提交表单?

[英]How to submit form without button?

I am new here.我刚来这地方。 I want my code to submit form without submit button.我希望我的代码在没有提交按钮的情况下提交表单。 My code run like this: Every time when user input data in input text and press enter, the result will be display on the same page just below input text.我的代码是这样运行的:每次当用户在输入文本中输入数据并按回车键时,结果将显示在输入文本正下方的同一页面上。 This code will work perfectly is I use submit button to submit data.如果我使用提交按钮提交数据,则此代码将完美运行。 Can anyone help me with this ?谁能帮我这个 ? Thanks in advance.提前致谢。

This is my result.html这是我的结果.html

$(document).ready(function(){
$("#code").change(function(){
    var st = '';
    $('#Myform input[type=text]').each(function(){

        st = st+ '<td>'+$(this).val()+'</td>';
        $(this).val('');
    });
    $('#result').append('<tr>'+st+'</tr>');
});

}); });

html

<form id="Myform">
    <table>
        <tr>
            <td>
                <p>Code:</p>
            </td>
            <td><input id="code" type="text" name="code" size="40" autofocus/></td>
        </tr>
    </table>
</form>
<table id="result"></table>

This is my code.php这是我的代码.php

<?php if( $_REQUEST["code"] ) {$code = $_REQUEST['code'];}?>

A cursory Google search would have brought up that jQuery provides a means of doing this.粗略的谷歌搜索会发现 jQuery 提供了一种方法。

You can trigger form submission via either the submit() or trigger() methods, ie您可以通过submit()trigger()方法触发表单提交,即

$('#Myform').submit()

or或者

$('#Myform').trigger('submit')

The former is merely a shortcut method to the latter.前者只是后者的捷径。

how are you?你好吗?

If you need to send to another php page, you can to send AJAX, if don't want to send, you can show data directly to your js, without ajax.如果需要发送到另一个php页面,可以发送AJAX,如果不想发送,可以直接向js显示数据,不需要ajax。

https://jsfiddle.net/5L9Leso8/ https://jsfiddle.net/5L9Leso8/

UPDATE:更新:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>teste</title>
</head>

<body>
    <div>
        <label for="test">
            <input type="text" name="test" id="test">
            <p id="rest"></p>
        </label>
    </div>
    <script>
    $('#test').on('change', function(e) {
        e.preventDefault();
        var self = $(this);

        /** if you don't need to send to your php code 
        $('#rest').html(self.val());
        **/
        if (self.val().length > 0) {
            $.ajax({
                url: 'yourUrlphp',
                type: 'post',
                data: {
                    yourText: self.val()
                },
                success: function(data) {
                    $('#rest').html(data);
                }
            });
        }
    });

    </script>
</body>

</html>

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

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