【问题标题】:jQuery - Change action and form ID and use thisjQuery - 更改操作和表单 ID 并使用它
【发布时间】:2017-06-17 05:30:15
【问题描述】:

显然在 DOM 中更改了值,甚至更改了表单的 ID,认为脚本不再有效,但是当您单击“发布”时,没有任何反应。搜索我尝试使用“prop”而不是“attr”,但它仍然不起作用。我认为这是因为“e.preventDefault()”。但我不知道如何禁用它。这是我的代码。 如您所见,一旦发送请求,输入和“发送”按钮将被停用,当接收到值时,它们会更改表单的 ID、按钮的 ID 和名称,并将其重命名为“发布”。

<script>
    jQuery(function($){
        var pluginUrl = '<?php echo plugin_dir_url( __FILE__ ); ?>' ;
        $('[id^="form-search"]').on('submit', function(e) {
            e.preventDefault();
            $.ajax({
                type : 'POST',
                url  : 'http://localhost/ladoserver/getapi.php',
                data : $(this).serialize(),
                cache: false,
                beforeSend: function(){
                    $('input#keyword').prop('disabled', true);
                    $('button#search').prop('disabled', true);
                    $('#resuls').html('<img src="'+pluginUrl+'../../assets/img/loading.gif" />');
                }
            }).done(function(data) {
                $('#results').html(data);
                $('input#keyword').prop('value', 'PUBLISH DATA');
                $('button#search').prop({disabled: false, id:'publish', name:'publish'}).html('Publish');
                $('span#help').hide();
                $('[id^="form-search"]').prop('action','publish.php');
                $('[id^="form-search"]').prop('id', 'form-publish'); 
                var countResults = $('input#total_data').val();
                for (var i = 1; i <= countResults; i++) {
                    $('#lista>select').clone().prop({ id: "cat_"+i, name: "cat_"+i}).appendTo('.category_'+i);
                }
                $('#lista').remove();
            });
        });
    });
</script>

这是我的html

        <form action="" method="post" class="form-search" id="form-search">
            <fieldset>
                <!-- Form Name -->
                <legend>Name</legend>
                <div class="form-group">  
                  <input id="keyword" name="keyword" required="" type="text">  
                  <button id="search" name="search" class="btn btn-success boton">Search</button>
                  <span class="help-block" id="help">Help</span>
                    <div id="lista" style="display:none">Test</div>
                </div>
            </fieldset>
            <div id="result"></div>
        </form>

这里有一个小提琴显示发生的事情(检查 DOM)。 JSFiddle.net

【问题讨论】:

  • 您能否发布 html 以及您的小提琴实际上不会从您当前发布的代码的角度阐明您想要实现的目标..
  • 好的,我添加了我的html。正如您所看到的,一旦我通过 AJAX 获得结果,我就更改了表单的“动作”以及按钮(名称和 ID),从而成为一个“新”表单。现在当我想给“发布”按钮时,这不起作用,我应该被重定向到 php 脚本页面。
  • ok..现在从您的代码中我可以看到您正在将操作和 id 属性添加到您的表单但是再次提交表单时它的处理程序在哪里我的意思是您应该再添加一个表单发布操作的处理程序
  • 我是使用 jQuery 的业余爱好者,我认为更改表单 ID 将被视为新的,激活新的发布按钮将作为普通表单处理。我该如何解决这个问题?
  • 好的..我会添加一些东西来帮助..

标签: javascript jquery html forms preventdefault


【解决方案1】:

            //var pluginUrl = '<?php echo plugin_dir_url( __FILE__ ); ?>' ;
            // when the action is plugin url.
            
            $(document).on('submit','[id^="form-search"]', function(e) {
                e.preventDefault();
                $.ajax({
                    type : 'POST',
                    url  : 'http://localhost/ladoserver/getapi.php',
                    data : $(this).serialize(),
                    cache: false,
                    beforeSend: function(){
                        $('input#keyword').prop('disabled', true);
                        $('button#search').prop('disabled', true);
                        $('#resuls').html('<img src="'+pluginUrl+'../../assets/img/loading.gif" />');
                    }
                }).done(function(data) {
                    $('#results').html(data);
                    $('input#keyword').prop('value', 'PUBLISH DATA');
                    $('button#search').prop({disabled: false, id:'publish', name:'publish'}).html('Publish');
                    $('span#help').hide();
                    $('[id^="form-search"]').prop('action','publish.php');
                    $('[id^="form-search"]').prop('id', 'form-publish'); 
                    var countResults = $('input#total_data').val();
                    for (var i = 1; i <= countResults; i++) {
                        $('#lista>select').clone().prop({ id: "cat_"+i, name: "cat_"+i}).appendTo('.category_'+i);
                    }
                    $('#lista').remove();
                });
            });

    // when the action point to publish.php    === This is what you need

      $(document).on('submit','[id^="form-publish"]', function(e) {
                e.preventDefault();
                var url = $(this).attr('action');
                $.ajax({
                    type : 'POST',
                    url  : url,
                    data : $(this).serialize(),
                    cache: false,
                    beforeSend: function(){
                        // do your pre-processing
                    }
                }).done(function(data) {
                    // do your post processing.
                });
            });        
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <form action="" method="post" class="form-search" id="form-search">
                <fieldset>
                    <!-- Form Name -->
                    <legend>Name</legend>
                    <div class="form-group">  
                      <input id="keyword" name="keyword" required="" type="text">  
                      <input type ="submit" id="search" name="search" class="btn btn-success boton">Search</button> 
    <!-- change the button type to submit type -->
                      <span class="help-block" id="help">Help</span>
                        <div id="lista" style="display:none">Test</div>
                    </div>
                </fieldset>
                <div id="result"></div>
            </form>

【讨论】:

  • 在第二个动作中我必须使用 AJAX 来处理我的 php 文件?如果操作是 url 会发生什么?
  • 没有必要拥有 ajax.. 我只是添加了它,因为你之前的调用是 ajax 所以假设你希望它是 ajax 并且通过 js 完成的原因是因为你没有任何提交按钮当您使用按钮时
  • 如果你不想要它 ajax 只需删除第二个表单提交处理程序,然后提交它将提交到新 url 的表单
  • 我实际上认为通过将“发送”按钮更改为“发布”,表单的行为就像一个正常的表单,当给出“发布”按钮时,“action = publish.php”将是执行。那我要在里面放什么:“$ ('[id ^ =" form-result "]'.) ('Submit', function (e) {}"?
  • 嘿,那很好..主要目的是对其进行排序,是的,实际上那是事件委托问题,基本上你需要重新绑定和绑定它,因为你想出了一个简单的解决方案,我想可能是我的解决方案尝试添加钢笔jsfiddle.net/55jtbhe0 .. 无论如何你有解决方案:D :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 2012-12-21
  • 2017-05-23
相关资源
最近更新 更多