简体   繁体   English

如何通过使用jQuery将表单数据添加到同一页面的引导程序模式中?

[英]How to add form data to a bootstrap modal in the same page by using jQuery?

I have a requirement and trying to write code for Where user enters/selects some information in form then clicks on Submit button then a popup window(bootstrap modal) appears with just one single input box where user just types a name for that selection. 我有一个要求,并尝试编写代码,以便用户在其中输入/选择表单中的某些信息,然后单击“提交”按钮,然后会出现一个弹出窗口(bootstrap modal),其中只有一个输入框,用户可以在其中输入该选择的名称。 What I am doing here is once user gives some information in form & after clicks on submit button I am storing that form before popup modal opens(in the middle of after user clicks on submit button and a popup window opens I am storing the given form inputs and fetching that last inserted record id and binding that id to a hidden input field in the popup modal), after popup window opens user types a name and clicks on Save button then I will be updating that record by using the same record id. 我在这里所做的是,一旦用户在表单中提供了一些信息,然后单击了提交按钮,我就在弹出模式打开之前存储了该表单(在用户单击提交按钮和弹出窗口之后的中间,我正在存储给定的表单输入并获取最后插入的记录ID,并将该ID绑定到弹出模式中的隐藏输入字段),在弹出窗口打开用户类型名称并单击“保存”按钮后,我将使用相同的记录ID更新该记录。 The problem Am facing here is after user enters all form data and he clicks on submit button I am storing this data in the database before a popup window opens, after popup window opens in popup window if user clicks on close or Esc button without giving a name then that form data is already saved with empty name becoz user didn't give a name in popup box because as he clicks on 'close' or 'Esc' button but my requirement is if user doesn't give a name then that record shouldn't be stored in the database. 用户输入所有表单数据并单击提交按钮后,我面临的问题是我将这些数据存储在数据库中,然后弹出窗口打开;如果用户单击关闭或Esc按钮但未给出名称,然后该表单数据已保存为空名称,因为用户未在弹出框中输入名称,因为当他单击“关闭”或“ Esc”按钮时,但我的要求是,如果用户未输入名称,则该记录不应存储在数据库中。

My Form code: 我的表格代码:

<form method="post" action="" id="my_form_id" class="my_form_class"> 
 <div class="col-md-6 col-sm-6 col-xs-12">
     <input type="radio" name="my_input_name_116" class="all select_option" data-id="116" value="all">
     <label for="all">All</label>
     <input type="radio" name="my_input_name_116" class="yes select_option" data-id="116" value="yes">
     <label for="yes">Yes</label>
     <input type="radio" name="my_input_name_116" class="no select_option" data-id="116" value="no" checked>
      <label for="no">No</label>
 </div>
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#rsModal" data-backdrop="static" data-keyboard="false" id="save_run_setup">SAVE RUN SETUP</button>

My bootstrap modal code: 我的引导程序模式代码:

<div class="modal fade" id="rsModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <!--<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>-->
                <h4 class="modal-title" id="myModalLabel">Run Setup Name</h4>
            </div>
            <div class="modal_failure run_setup_error">
                <p><span class="add_failure_msg"></span></p>
            </div>
            <div class="popup_success run_setup_success">
                <p><span class="add_success_msg"></span></p>
            </div>
            <form method="post" action="" id="add_rs_modal">
                <div class="modal-body">
                    <input type="hidden" class="form-control modal_user_id" id="user_id" name="user_id" value="">
                    <input type="hidden" class="form-control modal_rs_row_id" id="rs_row_id" name="rs_row_id" value="">
                    <div class="form-group">
                        <!--<label for="module-name">Run setup name</label>-->
                        <input type="text" class="form-control modal_runsetup_name" name="modal_runsetup_name" placeholder="Enter Run Setup Name" value="" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <!--<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>-->
                    <button type="button" class="btn btn-success" class="add_runsetup_name" id="add_runsetup_name">Save Run Setup</button>
                </div>
            </form>
        </div>
    </div>
</div>

My question here is how to carry the form input data along with a name which user is going to give in the popupbox, if user doesn't give a name then I will not save that previous form data? 我的问题是如何随身携带表单输入数据以及用户将要在弹出框中输入的名称,如果用户未提供名称,那么我将不会保存以前的表单数据? Any help would be appreciated. 任何帮助,将不胜感激。

Edited : 编辑:

What I am doing is after user gives the input to the form "my_form_id" he clicks on 'save_run_setup' button then in ajax call I am storing this form values into a table and getting the last inserted row id and putting that row id in popup modal(which opens after this ajax call) asking user to enter a name for this form selection and updating that revious record with this 'name' given in modal popup. 我正在做的是在用户将输入形式输入为“ my_form_id”之后,他单击“ save_run_setup”按钮,然后在ajax调用中,我将该表格值存储到表中,并获取最后插入的行ID,并将该行ID放入弹出窗口中modal(在此ajax调用之后打开),要求用户输入用于此表单选择的名称,并使用模式弹出窗口中给出的此“名称”更新该以前的记录。 But If user doesn't give a any name then I already saved a record after user clicks on 'Save Run Setup' button. 但是,如果用户未提供任何名称,则在用户单击“保存运行设置”按钮后,我已经保存了一条记录。 My requirement is I have to either append 'my_form_id' form data to bootstrap modal or modal input value to this form. 我的要求是我必须将'my_form_id'表单数据附加到该表单的引导模式或模式输入值。

If I got you correct 如果我答对了

my requirement is if user doesn't give a name then that record shouldn't be stored in the database. 我的要求是,如果用户不提供名称,则该记录不应存储在数据库中。

with bootstrap modal close event and validate the input field you can make sure that if user does't fill the input field either modal remain open and show a message and if user fill the input field you can run an Ajax call to add the data to database or do what ever you like to do with it.... 使用bootstrap modal close事件并验证输入字段,可以确保如果用户不填写输入字段,则其中一个modal保持打开状态并显示一条消息;如果用户填写了输入字段,则可以运行Ajax调用以将数据添加到数据库或做任何您想做的事...。

$('#rsModal').on('hide.bs.modal', function() {
  var UserName = $("#modal_runsetup_name").val();
  if (UserName == '') {
    alert('Enter User Name');
    return false;
  } else {
    alert('do what ever you like to do');
  }
});

Note you don't need "Save Run Setup" button in modal, modal close button will take care of everything. 注意,您不需要模态中的“保存运行设置”按钮,模态关闭按钮将处理所有事情。

Fiddle Example 小提琴的例子

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

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