简体   繁体   English

使用jQuery保存Popup数据

[英]Saving the Popup data using jQuery

I am having a drop down with "Create New" option. 我使用“新建”选项进行下拉。 If user clicks "Create New" option then popup window will display with save button. 如果用户单击“新建”选项,则将显示带有保存按钮的弹出窗口。 In that popup window, user will enter data and after user will save this data on server. 在该弹出窗口中,用户将输入数据,然后将其保存在服务器上。

This is jQuery function for save the data in a popup. 这是jQuery函数,用于将数据保存在弹出窗口中。

<script type = "text/javascript" >
  function savefile() {

    $(document).ready(function() {
      $('#Savefile').click(function() {
        $.ajax({
          url: 'D:\C#.Net\Scramble.txt',
          datatype: "json",
          method: 'post',
          data : scrtabledata,
          success: function() {
            var scrtabledata = $('#modaltable'); //Table ID
            scrtabledata.empty();
            $(data).each(function() {
              scrtabledata.append('<tr><td>' + scrtabledata.comment + '</td><td>' + scrtabledata.DropDownList2 + '</td><td>' + scrtabledata.DropDownList1 + '</td></tr>');
            });
          }
        });
      });
    });
  }; 
</script>

And passing that script like this, 然后像这样传递脚本

<button runat="server" id="Savefile" class="btn btn-primary" onclick="savefile()">Save </button>

This is the code for jQuery popup: 这是jQuery弹出窗口的代码:

<script type="text/javascript">
$(function () {
        //Attach click event to your Dropdownlist
        $("#<%= DropDownConfigFile.ClientID %>").change(function () {
            //Get the selected valu of dropdownlist
            selection = $(this).val();
            //If its one then show the dialog window. You can change this condition as per your need
            if (selection == 1) {
                //Show the modal window
                $('#myModal').modal('show');

            }
        });
    });
</script>

When user clicks "Save" button then data has to store in that given path. 当用户单击“保存”按钮时,数据必须存储在该给定路径中。 but it is not performing any action. 但它没有执行任何操作。 And it is closing automatically when I click "Save" button. 当我单击“保存”按钮时,它会自动关闭。 Can you please help me to solve this problem. 您能帮我解决这个问题吗?

Any wrong in this code? 这段代码有什么错误吗?

Try it: 试试吧:

<script type="text/javascript">
        function savefile() {
                    $.ajax({
                        url: 'D:\C#.Net\Scramble.txt',
                        datatype: "json",
                        method: 'post',
                        success: function () {
                            var scrtabledata = $('#modaltable');//Table ID
                            scrtabledata.empty();

                            $(data).each(function () {
                                scrtabledata.append('<tr><td>' + scrtabledata.comment + '</td><td>' + scrtabledata.DropDownList2 + '</td><td>' + scrtabledata.DropDownList1 + '</td></tr>');
                            });

                        }

                    });
        }
    </script>

You need to pass that data in the ajax function in an order to save it. 您需要在ajax函数中传递该数据才能保存它。

  $(document).ready(function () {
                $('#Savefile').click(function () {
                    $.ajax({
                        url: 'D:\C#.Net\Scramble.txt',
                        datatype: "json",
                        data : Your Data to be passed,
                        method: 'post',
                        success: function () {
                            var scrtabledata = $('#modaltable');//Table ID
                            scrtabledata.empty();

                            $(data).each(function () {
                                scrtabledata.append('<tr><td>' + scrtabledata.comment + '</td><td>' + scrtabledata.DropDownList2 + '</td><td>' + scrtabledata.DropDownList1 + '</td></tr>');
                            });

                        }

                     });
                });
            });
        }; 

here in the data attribute you need to pass your data in json format as key value pair. 在data属性中,您需要将json格式的数据作为键值对传递。

var myData={"id":"1","Name":"Test"}

In this way you can form your own JSON and pass it to your ajax function. 这样,您可以形成自己的JSON并将其传递给ajax函数。

You have to consider like if 你必须考虑

<input type="text" id="userId"/>
<input type="text" id="userData"/>

var userID=$('#userId').val();
var userData=$('#userData').val();
  $(document).ready(function () {
                $('#Savefile').click(function () {
                    $.ajax({
                        url: 'D:\C#.Net\Scramble.txt',
                        datatype: "json",
                        data : {
                                 id:userID,
                                 info:userData
                                 },
                        method: 'post',
                        success: function () {
                            var scrtabledata = $('#modaltable');//Table ID
                            scrtabledata.empty();

                            $(data).each(function () {
                                scrtabledata.append('<tr><td>' + scrtabledata.comment + '</td><td>' + scrtabledata.DropDownList2 + '</td><td>' + scrtabledata.DropDownList1 + '</td></tr>');
                            });

                        }

                     });
                });
            });
        }; 

Updated 更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM