如何在模态的部分视图中包含 Jquery 函数

发布于 2025-01-11 04:30:10 字数 1265 浏览 0 评论 0原文

我有一个 jquery 按钮单击方法,该方法在模态之外的视图中工作正常。在模态中加载部分视图时,uploadbtn 按钮单击方法不起作用

<script src="~/lib/jquery/dist/jquery.min.js"></script>

    <div class="form-group">
        <div class="col-md-10">
            <p>Upload one or more files using this form:</p>
            <input type="file" id="files" name="files" multiple />
            <input id="documentType" name="entity" value=@ViewData["entity"] hidden>
            <input id="id" name="id" value=@Model hidden>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-10">
            <input type="submit" id="upload" value="Upload" />
            <button type="button" id="uploadbtn">Upload</button>
            
        </div>
    </div>


<script>
    $(document).ready(function () {
        $("#uploadbtn").click(function () {
            console.log("came");
            var documentType = $('#documentType').val();
            var fileUpload = $("#files").get(0);
            var files = fileUpload.files;
            console.log(files);


          

        });
    });

    </script>

有人可以告诉我应该做什么才能在模态中工作吗? 我正在从事 .net core 项目

I have a jquery button click method which works fine in views other than modals.uploadbtn button click method doesn't work when partial view is loaded in the modals

<script src="~/lib/jquery/dist/jquery.min.js"></script>

    <div class="form-group">
        <div class="col-md-10">
            <p>Upload one or more files using this form:</p>
            <input type="file" id="files" name="files" multiple />
            <input id="documentType" name="entity" value=@ViewData["entity"] hidden>
            <input id="id" name="id" value=@Model hidden>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-10">
            <input type="submit" id="upload" value="Upload" />
            <button type="button" id="uploadbtn">Upload</button>
            
        </div>
    </div>


<script>
    $(document).ready(function () {
        $("#uploadbtn").click(function () {
            console.log("came");
            var documentType = $('#documentType').val();
            var fileUpload = $("#files").get(0);
            var files = fileUpload.files;
            console.log(files);


          

        });
    });

    </script>

Can someone tell me what I should do in order to get this working in the modals?
I am working in a .net core project

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2025-01-18 04:30:10

更新演示如下:

Controller.cs:我创建一个操作BBB来返回_FilePartialView

public class HomeController : Controller
   {
        public IActionResult Privacy()
        {
            return View();
        }
        public IActionResult BBB()
        {
            return PartialView("_FilePartialView");
        }
    }

在隐私视图中,我添加了一个上传文件的模式。

<button id="btnShowModal" type="button" > Upload </button>
<div class="modal fade" id="Modal">                                            
<div class="modal-dialog modal-lg">
 <div class="modal-content">
     <div class="modal-header">
        <button  id="btnHideModal" type="button" class="btn btn-secondary" data-dismiss="modal">  ×</button>
      </div>                                                             
    <div class="modal-body" >
</div>
    </div>
 </div>
</div>
script src="~/lib/jquery/dist/jquery.min.js"></script>
   

 <script type="text/javascript">
        $(document).ready(function () {
            $("#btnShowModal").click(function () {
                $.get("@Url.Action("BBB","Home")",
                function(data){$('.modal-body').html(data);})
                $("#Modal").modal('show'); 
            });
        $("#btnHideModal").click(function () {
                $("#Modal").modal('hide');
            });
        });       
    </script>  

_FilePartialView.cshtml:

[注意]我删除了

Update Demo as below:

Controller.cs: I create an action BBB to return _FilePartialView.

public class HomeController : Controller
   {
        public IActionResult Privacy()
        {
            return View();
        }
        public IActionResult BBB()
        {
            return PartialView("_FilePartialView");
        }
    }

In privacy view, I add a modal to upload file.

<button id="btnShowModal" type="button" > Upload </button>
<div class="modal fade" id="Modal">                                            
<div class="modal-dialog modal-lg">
 <div class="modal-content">
     <div class="modal-header">
        <button  id="btnHideModal" type="button" class="btn btn-secondary" data-dismiss="modal">  ×</button>
      </div>                                                             
    <div class="modal-body" >
</div>
    </div>
 </div>
</div>
script src="~/lib/jquery/dist/jquery.min.js"></script>
   

 <script type="text/javascript">
        $(document).ready(function () {
            $("#btnShowModal").click(function () {
                $.get("@Url.Action("BBB","Home")",
                function(data){$('.modal-body').html(data);})
                $("#Modal").modal('show'); 
            });
        $("#btnHideModal").click(function () {
                $("#Modal").modal('hide');
            });
        });       
    </script>  

_FilePartialView.cshtml:

[Note]I remove <script src="~/lib/jquery/dist/jquery.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文