简体   繁体   English

如何使用jQuery在模式弹出窗口中传递值

[英]How to pass values in a modal popup using jQuery

In a table I display several links whose value (user id) is extracted from a database. 在一个表中,我显示了几个链接,这些链接的值(用户ID)是从数据库中提取的。 By clicking on the link appears a modal pop up in which I would like to pass the selected value. 通过单击链接会出现一个模式弹出窗口,我想在其中传递所选值。

Here is the table: 表格如下:

<table>
    <tr>
        <td>Username</td>
        <td>Id</td>
    </tr>

<?php
    include ‘db_connection.php’;
    $sql = "SELECT * FROM users";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)){
?>
<tr>
    <td><?php echo $row[userID]?></td>
    <td>
        <div id='basic-modal'>
            <a href="?id=<?php echo $row[userID]?>"  class='basic'>Show</a>
        </div>
     </td>
</tr>

<?php } ?>

</table>

If I click on the link Show appears the modal pop up: 如果我单击链接,则显示将出现模式弹出窗口:

<div id="basic-modal-content">
    <?php
        if(isset($_GET[‘userID’])){
            $userID = $_GET[‘userID’];
            echo ‘UsuerID: ‘ .$userID;
        }
    ?>
</div>

This is the script I used 这是我使用的脚本

jQuery(function ($) {
    $('#basic-modal .basic').click(function (e) {
        $('#basic-modal-content').modal();
            return false;
    });
});

Since I have little familiarity with the jQuery framework I would like to ask you how can I pass the selected value within the modal and then use it. 由于我对jQuery框架不太熟悉,所以我想问你如何在模式中传递选定的值然后使用它。

jquery allows you to use the .data() attribute jQuery允许您使用.data()属性

    <div id='basic-modal'>
        <a href="?id=<?php echo $row[userID]?>"  data-mydata="<?php echo $row[userID]?>" class='basic'>Show</a>
    </div>

and then u can retrieve data from 'data-mydata' attribute: 然后您可以从'data-mydata'属性检索数据:

jQuery(function ($) {
    $('#basic-modal .basic').click(function (e) {
        $('#basic-modal-content')
              .text($(this).data('mydata'))
               .modal();
            return false;
    });
});

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

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