【问题标题】:how to add image upload along with ajax jquery form submit如何添加图像上传以及ajax jquery表单提交
【发布时间】:2013-02-10 06:05:34
【问题描述】:

我正在使用 ajax jquery 提交表单。我想上传一张图片和其他字段。

是否可以对我当前的脚本进行更改以完成它,或者我需要重做所有事情:-

我的 jquery 是:-

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>

<script type="text/javascript">
    $(function () {
        $(".submit").click(function () {
            var article_title = $("#article_title").val();
            var article_body = $("#article_body").val();

            var dataString = 'article_title=' + article_title + '&article_body=' + article_body;

            if (article_title == '' || article_body == '') {
                $('.success').fadeOut(200).hide();
                $('.error').fadeOut(200).show();
            } else {
                $.ajax({
                    type: "POST",
                    url: "<?php echo site_url().'/main/submit_article'?>",
                    data: dataString,
                    success: function () {
                        //$('.success').fadeIn(200).show();
                        $('#article_form')[0].reset();
                        $('.error').fadeOut(200).hide();
                    }

                });
            }

            return false;
        });
    });
</script>

我的 HTML 表单:-

<form name="article_form" id="article_form" class="article_form" method="POST" action="">
    <input type="text" name="article_title" id="article_title" placeholder="Title for your article" /> <!--<input type="file" name="pic" />-->
    <br>
    <textarea rows="12" name="article_body" id="article_body" placeholder="Tell your story"></textarea>
    <br>
    <input type="submit" name="submit" id="submit" value="Post" class="submit"/>
</form>

【问题讨论】:

  • 你需要有帮助的 jQuery.form 插件
  • 我没听懂你。它检查字段是否为空。到目前为止,这正在工作。
  • @DipeshParmar 我是否可以将它与我编写的脚本集成而无需进行重大更改
  • 是的,您可以...malsup.com/jquery/form 不需要进行重大更改..
  • @DipeshParmar 嘿,我无法正确使用带有 codeigniter 的 malsup。它加载一个单独的页面 - stackoverflow.com/questions/15121370/…

标签: jquery ajax codeigniter image-upload


【解决方案1】:

为什么不使用jQuery.form插件。

这个插件可以有效地处理文件上传Check here。

用于清除字段

$(':input','#myform').not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');

【讨论】:

  • Parmer 您好,请您帮我在提交表单后清除表单域。我认为应该使用 clearForm,但我不确定如何
  • 我试过这个:- var options = { clearForm: true } 它不工作
  • @StacyJ 查看我的答案更新..在成功回调中调用它
  • 不,它不工作。我将#myform 更改为#myForm,因为我的表单的名称和ID 是myForm。我在 $('#myForm').ajaxForm(function() { //alert("Thank you for your comment!"); }); 之后粘贴了它
  • 然后我在 $(document).ready(function() 关闭后尝试了它。仍然没有运气
【解决方案2】:

将 enctype="multipart/form-data" 添加到您的表单元素中。

【讨论】:

    【解决方案3】:

    让我在这里给你一个节省时间的方法,试试看:

    上传http://www.uploadify.com/

    或者你可以试试:

    Jquery 表单malsup.com/jquery/form/

    如果你需要进度条,多次上传,拖拽 https://github.com/valums/file-uploader

    使用这些工具和插件可以减轻压力。除非您有更好的想法,否则不要重新发明轮子。

    【讨论】:

      猜你喜欢
      • 2013-06-22
      • 1970-01-01
      • 2011-12-10
      • 2021-08-02
      • 1970-01-01
      • 2017-03-03
      • 2011-05-07
      • 1970-01-01
      • 2019-09-24
      相关资源
      最近更新 更多