【问题标题】:Where to add validation with JQuery-UI AJAX PHP?在哪里使用 JQuery-UI AJAX PHP 添加验证?
【发布时间】:2013-03-04 17:12:17
【问题描述】:

我已经尝试了所有我能想到的在创建表单 DOM 对象后添加验证的变体。代码本身可以打开和关闭 jquery-ui.dialog。我只是不知道在处理程序中的何处拼接以进行任何验证。

 //party_device_edit Begin 
    var party_device_edit = $('#party_device_edit').dialog({
      autoOpen: false,  width:'auto',  modal: true, title: 'Edit Device', 
      buttons: {
        'Save': function(){
**//Tried Adding Validation Here**
            $.ajax({
            url: '/party_device/save.php',
            type: 'POST',
            data: $('#party_device_edit_form').serialize(),
            success: function(data){
              //Re-queries the data and refreshes the piece of the page that has updated data.
              $.get('helper.php?function=party_device&id=<?php echo $_GET['id'];?>',function(data){$('#party_device').html(data);});
              $('#party_device_edit').dialog('close');
            }
          });
        return false;
        }
      }
    });
    $(document).on('click', '.party_device_edit', function (e) {
        e.preventDefault();
        $.ajax({
        url: $(this).attr('href'),
        type: 'POST',
        success: function(data){
          party_device_edit.html(data);
**//Tried Adding Validation Here**
          party_device_edit.dialog('open');
**//Tried Adding Validation Here**
        }
      });
    });
    //party_device_edit End

【问题讨论】:

  • 显示相关的 HTML 也会对您有所帮助。

标签: jquery ajax jquery-ui jquery-validate


【解决方案1】:

You put the ajax inside the submitHandler in the Validate plugin,然后将.submit() 分配给您的模态框的“保存”按钮。

$(document).ready(function () {

    $("#party_device_edit_form").validate({
        // // validate rules & options,
        submitHandler: function (form) {
            $.ajax({
                url: '/party_device/save.php',
                type: 'POST',
                data: $(form).serialize(),
                success: function (data) {
                    //Re-queries the data and refreshes the piece of the page that has updated data.
                    $.get('helper.php?function=party_device&id=<?php echo $_GET['
                    id '];?>', function (data) {
                        $('#party_device').html(data);
                    });
                    $('#party_device_edit').dialog('close');
                }
            });
            return false;
        }
    });

    var party_device_edit = $('#party_device_edit').dialog({
        autoOpen: false,
        width: 'auto',
        modal: true,
        title: 'Edit Device',
        buttons: {
            'Save': function () {
                $("#party_device_edit_form").submit();
            }
        }
    });

});

由于您未能显示您的 HTML,我无法创建特定的演示,但这里有一个与您的 非常 相似的测试用例...

演示:http://jsfiddle.net/7bA8M/

【讨论】:

  • 我很欣赏你的榜样。我试图从这里实现您的示例以及您在 jsfiddle 中的内容。但是,我现在坚持如何让提交的表单发布到 ajax 调用而不是 url。我想我在某处缺少 .preventDefault 。表单的 HTML 是通过 jquery-ui.dailog 动态创建的。
  • @MattSmith,是的,那是你的问题。调用.validate() 时,必须 形式的 HTML 存在。因此,只需将整个 .validate() 函数移动到创建表单 HTML 的相同位置即可。将它立即放在 创建 HTML 的代码之后。
  • Sparky 修复了我遇到的主要问题,谢谢。最终的工作代码如上。
猜你喜欢
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
相关资源
最近更新 更多