【问题标题】:pass value to modal popup using jquery by using data-id attribute in anchor tag通过在锚标记中使用 data-id 属性,使用 jquery 将值传递给模式弹出窗口
【发布时间】:2015-12-05 05:15:02
【问题描述】:
<div class="login-group">
<div class="form-group">
    <table cellspacing="0" cellpadding="0" border="0" class="container">
        <?php
        $selectquery = "Select * from tblservices where category_id = 1";
        $qry=mysqli_query($con,$selectquery);
        if($qry)
        {
            $rowcount=mysqli_num_rows($qry);
            if($rowcount>0)
            {
                $countI = 1;
                while($obj=mysqli_fetch_array($qry))
                {
                    if($countI==0)
                    {
                        ?>
                        <tr>
                            <td class="bgimg">
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <?php echo $obj["service_name"]; ?>
                                </a>
                            </td>
                            <td>
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <img src="<?php echo '../assets/img/'.$obj['service_image']; ?>" alt="" >
                                </a>
                            </td>
                        </tr>
                        <?php
                        $countI = $countI + 1;
                    }
                    else if($countI %2 ==0)
                    {
                        ?>
                        <!--<a href="#" data-id="<?php echo $obj["value1"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">show value</a>-->
                        <tr>
                            <td>
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <img src="<?php echo '../assets/img/'.$obj['service_image']; ?>" alt="" >
                                </a>
                            </td>
                            <td style="font-size:20px; text-align: center;" class="bgimg">
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <?php echo $obj["service_name"]; ?>
                                </a>
                            </td>
                        </tr>
                        <?php
                        $countI = $countI + 1;
                    }
                    else
                    {
                        ?>
                        <tr>
                            <td style="font-size:20px; text-align: center;" class="bgimg">
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <?php echo $obj["service_name"]; ?>
                                </a>
                            </td>
                            <td>
                                <a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
                                    <img src="<?php echo '../assets/img/'.$obj['service_image']; ?>" alt="" >
                                </a>
                            </td>
                        </tr>
                        <?php
                        $countI = $countI + 1;
                    }
                }
            }
        }
        ?>
    </table>
</div>

上面的代码显示了来自数据库的值,其中 data-id 作为锚标签中的服务 id

这个数据ID应该显示在模式弹出窗口中,在下面找到

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="padding-top: 150px;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Choose your slot</h4>
            </div>
            <div class="modal-body">
                <input type="text" name="service_id" class="hiddenid"/>
                <div class="form-group">
                    <table align="center">
                        <tr>
                            <td colspan="2"> <p class="white_text">Preferred slot 1</p></td>
                        </tr>
                        <tr>
                            <td style="padding-right: 10px;"><input type="date" id="theDate" name="slot1_dt" required> </td>
                            <td><input type="time" id="timePicker1" name="slot1_tm" required> </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="padding-top: 15px;"><p class="white_text">Preferred slot 2</p></td>
                        </tr>
                        <tr>
                            <td style="padding-right: 10px;"><input type="date" id="theTomorrow" name="slot2_dt" required> </td>
                            <td><input type="time" id="timePicker2" name="slot2_tm" required> </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="padding-top: 15px;">
                                <center><button type="submit" name="book" class="btn btn-default" value="book" style="font-size: 14px !important;">Book</button>
                                &nbsp;
                                <button type="button" class="btn btn-default" style="font-size: 14px !important;" data-dismiss="modal">Close</button></center>
                            </td>
                        </tr>
                    </table>

                    <?php
                    if(isset($_REQUEST["book"]))
                    {
                        if($_REQUEST["book"])
                        {
                            $service_id=$_REQUEST["service_id"];
                            $customer_id=$_REQUEST["cust_id"];
                            $slot1_dt=$_REQUEST["slot1_dt"];
                            $slot2_dt=$_REQUEST["slot2_dt"];
                            $slot1_tm=$_REQUEST["slot1_tm"];
                            $slot2_tm=$_REQUEST["slot2_tm"];
                            $slot1=$slot1_dt." ".$slot1_tm;
                            $slot1 = date("Y-m-d H:i:s",strtotime($slot1));
                            $slot2=$slot2_dt." ".$slot2_tm;
                            $slot2 = date("Y-m-d H:i:s",strtotime($slot2));
                            $insertqry="INSERT INTO `tblappointments`(`customer_id`, `service_id`, `preferred_slot1_date`, `preferred_slot2_date`)
                            VALUES ('$customer_id','$service_id','$slot1','$slot2')";
                            $res=mysqli_query($con, $insertqry) or die(mysqli_error($con));

                            if($res)
                            {
                                echo("successful.....");
                            }
                        }
                    }
                    ?>
            </div>
        </div>
        <div class="modal-footer"></div>
    </div>
</div>
</div>

用于传值的jquery是

<?
$(".modalLink").click(function () {
var passedID = $(this).data('id');
$('#id').val(passedID);

//modifies input in modal
$(".modal-body .hiddenid").val(passedID);});
?>

这些代码在单个 php 文件下,js 存储为单独的文件

在输出上我无法获取要传递的数据 ID 值

文本字段仍然为空。

请大家帮帮我。

【问题讨论】:

标签: javascript php jquery html modalpopup


【解决方案1】:

首先,用href="#myModal".删除href="#"

<a href="#" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
    <img src="<?php echo '../assets/img/'.$obj['service_image']; ?>" alt="" >
</a>

其次,这不是打电话data-id.的方式 删除 var passedID = $(this).data('id'); 和 var dataId=$(this).attr('data-id'); 如下所示。

<?
$(".modalLink").click(function () {
var passedID=$(this).attr('data-id');
.
.
?>

如果它有效,那就太好了。如果没有,请按照我的代码一步一步来,它会起作用。

所以,我建议你只为模式制作一页(somepage.php)。使用ajax 以适当方式传递“dataId”的位置。

1) 如下写你的&lt;a&gt;&lt;/a&gt; 标签。 href="#myModal" 是必填项。

<a class="modalLink" href="#myModal" data-toggle="modal" data-id="<?php echo $obj["service_id"]; ?>">
    <?php echo $obj["service_name"]; ?>
</a>

2) 在页脚中,放置此代码。 (如 footer.php)

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="padding-top: 150px;">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div>

3) 通过 ajax 调用您的“somepage.php”(单独的页面。模态正文所在的位置)。将此 &lt;script&gt;&lt;/script&gt; 放在您的 JS 文件中。

<script>
$('.modalLink').click(function(){
    var dataId=$(this).attr('data-id');
    $.ajax({url:"somepage.php?dataId="+dataId,cache:false,success:function(result){
        $(".modal-content").html(result);
    }});
});
</script>

4) 创建somepage.php(如果你想更改此页面名称。也更改&lt;script&gt;&lt;/script&gt;。两者都是相关的。)

somepage.php

<?

// Access $dataId like this way and use any where you want in this modal. 

$dataId=$_GET['dataId'];
?>


<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title" id="myModalLabel">Choose your slot</h4>
</div>
<div class="modal-body">
    <input type="text" name="service_id" class="hiddenid" value="<?echo $dataId;?>"/>
    <div class="form-group">
        <table align="center">
            <tr>
                <td colspan="2"> <p class="white_text">Preferred slot 1</p></td>
            </tr>
            <tr>
                <td style="padding-right: 10px;"><input type="date" id="theDate" name="slot1_dt" required> </td>
                <td><input type="time" id="timePicker1" name="slot1_tm" required> </td>
            </tr>
            <tr>
                <td colspan="2" style="padding-top: 15px;"><p class="white_text">Preferred slot 2</p></td>
            </tr>
            <tr>
                <td style="padding-right: 10px;"><input type="date" id="theTomorrow" name="slot2_dt" required> </td>
                <td><input type="time" id="timePicker2" name="slot2_tm" required> </td>
            </tr>
            <tr>
                <td colspan="2" style="padding-top: 15px;">
                    <center><button type="submit" name="book" class="btn btn-default" value="book" style="font-size: 14px !important;">Book</button>
                    &nbsp;
                    <button type="button" class="btn btn-default" style="font-size: 14px !important;" data-dismiss="modal">Close</button></center>
                </td>
            </tr>
        </table>

        <?php
        if(isset($_REQUEST["book"]))
        {
            if($_REQUEST["book"])
            {
                $service_id=$_REQUEST["service_id"];
                $customer_id=$_REQUEST["cust_id"];
                $slot1_dt=$_REQUEST["slot1_dt"];
                $slot2_dt=$_REQUEST["slot2_dt"];
                $slot1_tm=$_REQUEST["slot1_tm"];
                $slot2_tm=$_REQUEST["slot2_tm"];
                $slot1=$slot1_dt." ".$slot1_tm;
                $slot1 = date("Y-m-d H:i:s",strtotime($slot1));
                $slot2=$slot2_dt." ".$slot2_tm;
                $slot2 = date("Y-m-d H:i:s",strtotime($slot2));
                $insertqry="INSERT INTO `tblappointments`(`customer_id`, `service_id`, `preferred_slot1_date`, `preferred_slot2_date`)
                VALUES ('$customer_id','$service_id','$slot1','$slot2')";
                $res=mysqli_query($con, $insertqry) or die(mysqli_error($con));

                if($res)
                {
                    echo("successful.....");
                }
            }
        }
        ?>
    </div>
</div>
<div class="modal-footer">

</div>

享受编码。

更新代码(根据@Sridhar 的要求)

1) 将 href="#" 更改为 href="#myModal" 在您的代码中出现的任何地方以弹出模式。

<td class="bgimg">
  <a href="#myModal" data-id="<?php echo $obj["service_id"]; ?>" data-toggle="modal" data-target="#myModal" class="modalLink">
      <?php echo $obj["service_name"]; ?>
  </a>
</td>

2) 在 JS 中使用此代码。

JS

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$('.modalLink').click(function(){
  var ID=$(this).attr('data-id');
  $.ajax({url:"NewPage.php?ID="+ID,cache:false,success:function(result){
      $(".modal-content").html(result);
  }});
});
</script>

3) 将此代码放在页脚中。

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="padding-top: 150px;">
  <div class="modal-dialog">
    <div class="modal-content">

        </div>
    </div>
</div>

4) 创建NewPage.php(如果要更改此页面的页面名称。请在Point-2 &lt;script&gt;&lt;/script&gt;标签中也更改。两者都是相关的)

NewPage.php

通过_GET 访问ID 并使用此ID 做任何你想做的事情

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title" id="myModalLabel">Choose your slot</h4>
</div>
<div class="modal-body">
  <?php echo $_GET['ID'];?>
</div>
<div class="modal-footer"></div>

【讨论】:

  • 您好,先生,感谢您的回答,现在模态弹出窗口不起作用,点击时只显示一行
  • 我正在尝试解决这个问题,如果有人知道请更新,这将非常有用
  • 嗨@SridharG.K:我更新了我的代码。请看一看。我想,现在我你不会遇到任何问题。 干杯
猜你喜欢
  • 1970-01-01
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多