简体   繁体   English

HTML5表单提交

[英]HTML5 Form Submit

I am trying to get a form to submit an action to an ip address without open the ip address. 我正在尝试获取一个表单,以在不打开IP地址的情况下向IP地址提交操作。

So, when I hit the submit button I want it to send the post command to the ip (in this case 192.168.0.1) and just refresh the current page. 所以,当我点击提交按钮,我希望它的post命令发送到IP(在这种情况下192.168.0.1),只是刷新当前页面。

<div data-role="main" class="ui-content">
<form method="POST" action="http://192.168.0.1/">
<input type="submit" name="parser" value="thisguy"/>
</form>
</div>

My script that runs on submit: 我的脚本在提交时运行:

<script src="http://ajax.googleapis.com/ajax/liubs/jquery/1.9.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
    $('form.ajax').on('submit', function() {
        var that = $(this),
            url = that.attr('action'),
            method = that.attr('method'),
            data - {};

        that.find('[name]').each(function() {
            var that = $(this),
                name = that.attr('name'),
                value = that.val();
            data[name] = value;

            $.ajax({
                url: url,
                type: method:,
                data: data,
                success: function(response) {}

            });

        });

        return false;
    });
</script>

Right now it submits the post and tries to open 192.168.0.1 as a webpage. 现在,它提交帖子并尝试打开192.168.0.1作为网页。 Can someone help me out either by providing code with an explanation or pointing me to the command? 有人可以帮我要么通过提供代码的解释或指向我的命令?

Any help is appreciated. 任何帮助表示赞赏。

Well, you can refresh the page after the POST is done in success method 好了,您可以在success完成POST方法后刷新页面

success: function(response) {
  window.location.reload();
}

Also do not forget to disable the form default behavior with 也不要忘记使用以下方式禁用表单默认行为

 $('form.ajax').on('submit', function(event) {
   event.preventDefault();
   .
   .
 });

See documentation for more info 请参阅文档以获取更多信息

I have done correction to your code. 我已经对您的代码进行了更正。

<script language="javascript" type="text/javascript">
    $('form').on('submit', function(e) {
        e.preventDefault();
        var that = $(this),
            url = that.attr('action'),
            method = that.attr('method'),
            data = {};

        that.find('[name]').each(function() {
            var that = $(this),
                name = that.attr('name'),
                value = that.val();
            data[name] = value;

            $.ajax({
                url: url,
                type: method,
                data: data,
                success: function(response) {}

            });

        });

        return false;
    });
</script>

You need to prevent the default action (using e.preventDefault() ) 您需要阻止默认操作(使用e.preventDefault()

$('form.ajax').on('submit', function(e) {
    e.preventDefault(); // Prevent default submit action (that is, redirecting)
    var that = $(this),
        url = that.attr('action'),
        method = that.attr('method'),
        data = {};

    that.find('[name]').each(function() {
        var that = $(this),
            name = that.attr('name'),
            value = that.val();
        data[name] = value;

        $.ajax({
            url: url,
            type: method:,
            data: data,
            success: function(response) {}

        });

    });

    return false;
});

To enable debugging, you should wrap the whole submit handler in a try/catch because without it, errors will cause the default handler to submit the form, masking the errors. 要启用调试,应将整个提交处理程序包装在try / catch中,因为如果没有它,错误将导致默认处理程序提交表单,从而掩盖错误。 After the try/catch you can return false, so the page stays put. 在try / catch之后,您可以返回false,因此页面保持原样。

<script src="http://ajax.googleapis.com/ajax/liubs/jquery/1.9.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
    $('form.ajax').on('submit', function() {
        try {
            var that = $(this),
                url = that.attr('action'),
                method = that.attr('method'),
                data - {};

            that.find('[name]').each(function() {
                var that = $(this),
                    name = that.attr('name'),
                    value = that.val();
                data[name] = value;

                $.ajax({
                    url: url,
                    type: method:,
                    data: data,
                    success: function(response) {}

                });

            });
        }
        catch (err) {
            console.log( 'submit handler error: ' + err.message );
        }

        return false;
    });
</script>

You just had couple of typo, like - instead of = and extra : , else your code is fine, below is working example. 你只是有几个错字,就像-而不是=和额外的: ,否则你的代码是好的,以下是工作的例子。

 $(function(){ $('form.ajax').on('submit', function() { var that = $(this), url = that.attr('action'), method = that.attr('method'), data = {}; that.find('[name]').each(function() { var that = $(this), name = that.attr('name'), value = that.val(); data[name] = value; $.ajax({ url: url, type: method, data: data, success: function(response) { alert('posted') } }); }); return false; }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div data-role="main" class="ui-content"> <form method="POST" action="http://192.168.0.1/" class="ajax"> <input type="submit" name="parser" value="thisguy"/> </form> </div> My script that runs on submit: 

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

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