如何在模态内容中调用javascript函数?

时间:2016-07-21 10:30:56

标签: javascript php ajax

我有两个带引导程序的php页面。在第一页中,我调用了一个用于插入每个员工数据的模式。第二页包含模态内容,它通过AJAX从第一页调用。

这是我的代码。

第1页:

<table data-toggle="table"  data-show-refresh="true" data-show-toggle="true" data-show-columns="true" data-search="true" data-select-item-name="toolbar1" data-pagination="true" data-sort-name="name" data-sort-order="desc">
<thead>
 <tr>
    <th data-field="company" data-sortable="true" data-align="center">Company Name</th>
    <th data-field="owner" data-sortable="true" data-align="center">Owner Name</th>
    <th data-field="contact" data-sortable="true" data-align="center">Contact No</th>
    <th data-field="category" data-sortable="true" data-align="center">Category</th>
    <th data-field="payment" data-sortable="true" data-align="center">Payment</th>
    <th data-field="calendar" data-sortable="true" data-align="center">Calendar</th>
 </tr>
</thead>
<tbody>
    <?php
      $sql_user="SELECT * from empployee order by id desc ";
      $result_user=mysqli_query($con,$sql_user);
      while($user=mysqli_fetch_array($result_user,MYSQLI_ASSOC))
      {
    ?>
    <tr>
     <td><?php echo $user['company']; ?></td>
     <td><?php echo $user['firstname']." ".$user['lastname']; ?></td>
     <td><?php echo $user['mobile_number']; ?></td>
     <td><?php echo $user['desig']; ?></td>
     <td><?php echo $user['salary']; ?></td>
     <td><button type="button" class="btn btn-info btn-sm"  data-toggle="modal" data-target='<?php echo $id2;?>' value='<?php echo $user['company'];?>' onclick="add_lineup(this.value);"><i class="fa fa-pencil"></i>Add</button>
         <div class="modal fade" id="<?php echo $im2;?>" role="dialog" aria-hidden="true">
            <div class="modal-dialog modal-lg">
              <div class="modal-content" id='<?php echo $edit;?>'>
              </div>
            </div>
         </div></td></tr>     
</tbody>
</table>

2页:

<?php
    session_start();
    if(!isset($_SESSION['admin']))
    {
     header("location:../index.php");
    }
        include('../connection.php');
        $details_id = $_REQUEST['details_id'];
        $company_sql =  mysqli_query($con,"SELECT * FROM task where id='$details_id'");
        $data_company = mysqli_fetch_array($company_sql);
    ?>
    <form action="" method="post"  enctype="multipart/form-data">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">Task Details</h4>
    </div>
        <input type="hidden" name="id" value="<?php echo $details_id ?>">
        <div class="modal-body">
        <table data-toggle="table"  data-show-refresh="true" data-show-toggle="true" data-show-columns="true" data-search="true" data-select-item-name="toolbar2" data-pagination="true" data-sort-name="name2" data-sort-order="desc">
            <thead>
                <tr>
                    <th data-field="name" data-sortable="true" >Item Name</th>
                    <th data-field="days" data-sortable="true" >Days</th>
                    <th data-field="Select" data-sortable="true">Select</th>
                    <th data-field="date" data-sortable="true">Delivery Date</th>
      </tr>
            </thead>
            <tbody>
                <?php
                    $search_task_items = "select * from task";
                    $run_search_task_items = mysqli_query($con, $search_task_items);
                    if(!$run_search_task_items)
                    {
                        die(mysqli_error($con));
                    }
                    while($get_task_items = mysqli_fetch_array($run_search_task_items))
                    {
                ?>
                        <tr>
                        <td><?php echo $get_task_items['item_name']; ?></td>
                        <td><?php echo $get_task_items['req_days']; ?></td>
                        <td><input type="checkbox" id="select[]" value="<?php echo $get_task_items['id']; ?>" onclick="test()"></td>
                        <td><input type="text" id="delivery_date" value=""></td></tr>
                <?php
                    }
                ?>
            </tbody>
        </table>

        <div class="modal-footer">
            <button type="submit" class="btn btn-primary pull-right">Add Customer Line-up Items</button>
        </div>
    </form>

我的问题是,如果单击复选框,我想在第二页上执行一些javascript功能。请记住,第二页内容将使用AJAX以模态显示。

我希望我的问题很明确。如果需要任何进一步的信息,我准备提供,但我想要这个答案。

谢谢。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,那么您只需要在复选框上添加onclick事件

onclick="if ($('input.checkbox_check').is(':checked')) {  //call your function here }"

如果是多个复选框,只需使用&#34;这个&#34;操作

onclick="if ($(this).is(':checked')) {  //call your function here }"