javascript
  • php
  • jquery
  • html
  • forms
  • 2014-09-19 83 views 1 likes 
    1

    所以,我试图做一个包含删除按钮以删除按钮后。 问题是..删除按钮提交表单,我不能预先定义我的按钮类/ ID,因为它是多次beeing echo'd(php脚本读取目录,然后将所有文件放在一个表中)表单提交后隐藏表格内的按钮?

    这是当前脚本,它在后台发布表单,但它不隐藏元素,我自己尝试了一些东西,但是脚本最终隐藏了页面上的所有按钮,或者它不起作用所有..

    按钮beeing回显:

    echo "<input type='submit' value='delete' name='submit'>"; 
    

    其背后的脚本:

    $("#delform").submit(function() { 
    
        var url = "../../setdel.php"; // the script where you handle the form input. 
    
        $.ajax({ 
          type: "POST", 
          url: url, 
          data: $("#delform").serialize(), // serializes the form's elements. 
          success: function(data) 
          { 
          // location.reload(); // show response from the php script. 
          } 
         }); 
    
        return false; // avoid to execute the actual submit of the form. 
    }); 
    
    +0

    @PHPglue OP使用'submit'按钮,这通常会导致网页提交/刷新反正 – jasonscript 2014-09-19 01:31:32

    +0

    我不知道任何其他方式,我脚本从当前登录的用户读出目录,然后显示表中的所有文件,并且我知道如何处理删除的唯一方法是使用提交给正在删除所选文件的php文件的表单。 – Sjef92 2014-09-19 01:42:40

    回答

    0

    抓住按钮的onclick而不是捕获提交。例如:

    echo "<input type='submit' value='delete' name='delete' class='trigger_delete'>"; 
                  // ^^ don't name your buttons as submit 
                  // it will conflict to .submit() method 
    

    上JS:

    $('.trigger_delete').on('click', function(e){ 
        e.preventDefault(); // prevent triggering submit 
        var url = "../../setdel.php"; 
    
        $.ajax({ 
         type: 'POST', 
         url: url, 
         data: $("#delform").serialize(), 
         success: function(response) { 
          console.log(response); 
          $(e.target).closest('tr').hide(); // or .fadeOut(); 
         }(e) // <--- this one 
        }); 
    }); 
    
    +0

    它确实删除了,但是这只隐藏了此表中的所有删除按钮,行仍然存在:) – Sjef92 2014-09-19 01:40:22

    +0

    @ Sjef92好吧,我删除了,检查我的修订版,我忘记引用触发按钮的对象,试试它再次 – Ghost 2014-09-19 01:50:01

    +0

    仍然只有删除按钮,这次不是所有的删除按钮,但只有一个指定的按钮。所以这是进步! :D – Sjef92 2014-09-19 02:01:27

    相关问题