简体   繁体   English

在模态引导中使用jquery或javascript传递数据

[英]Passing data using jquery or javascript in modal bootstrap

I have a table that represented from SQL Table that looping all data with PHP. 我有一个用SQL表表示的表,该表用PHP循环了所有数据。

HTML 的HTML

<tbody>
   <?php
    $no = 1;
    foreach($data_request as $data) {
    ?>

  <tr>
    <td class="center"><?php echo $no++.". ";?> </td>                                           
    <td class="sorting1"><?php echo $data['id_request'];?> </td>
    <td class="center"><?php echo "$nama"; ?></td>                               
    <td  class="right">
       <a class="btn btn-danger" href="#">
         <i class="halflings-icon white trash"></i> Close</a>
       <a class="btn btn-success" href="#" id="print"?>">
         <i class="halflings-icon pencil"></i> Preview  </a>         
   </td>
  </tr>

   <?php } ?>
   </tbody>
   </table>  


  <div class="modal hide fade" id="myModal">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h3>Print Preview</h3>
        </div>
        <div class="modal-body">

            /*This would be a preview*/


        </div>
        <div class="modal-footer">
             <a href="#" class="btn btn-primary">Make it to PDF</a>
            <a href="#" class="btn" data-dismiss="modal">Close</a>

        </div>
    </div>

My problem is, I wanna display a row data to modal bootstrap if users clicked the preview button. 我的问题是,如果用户单击预览按钮,我想显示行模态引导数据。 So, if user click the button in row 1, modal would be displaying all field/data in first row of table. 因此,如果用户单击第1行中的按钮,则模式将在表的第一行中显示所有字段/数据。 Untill now, I have success passing data using jquery like this : 到目前为止,我已经成功使用jquery这样传递数据了:

 <script>
        $('.btn-success').click(function(){
            var address = [];
                $(this).closest('tr').find('td').not(':last').each(function() {

                var textval = $(this).text(); // this will be the text of each <td>
                address.push(textval);
            });

            alert(address.join('\n'));
        });

    </script>

I am confusing to pass the data from script to modal, anyone can help ? 我很困惑将数据从脚本传递到模式,有人可以帮忙吗?

Better to give unique ids to the rows while rendering it from SQL DB. 从SQL DB渲染行时最好给行赋予唯一的ID。

<tbody>
   <?php
    $no = 1;
    foreach($data_request as $data) {
    ?>

  <tr id="row_<?php echo $data['id_request']; ?>">
    <td class="center"><?php echo $no++.". ";?> </td>                                           
    <td class="sorting1"><?php echo $data['id_request'];?> </td>
    <td class="center"><?php echo "$nama"; ?></td>                               
    <td  class="right">
       <a class="btn btn-danger" href="#">
         <i class="halflings-icon white trash"></i> Close</a>
       <a class="btn btn-success" href="#" id="print_<?php echo $data['id_request']; ?>"">
         <i class="halflings-icon pencil"></i> Preview  </a>         
   </td>
  </tr>

   <?php } ?>
   </tbody>
   </table>  


  <div class="modal hide fade" id="myModal">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h3>Print Preview</h3>
        </div>
        <div class="modal-body">

            /*This would be a preview*/


        </div>
        <div class="modal-footer">
             <a href="#" class="btn btn-primary">Make it to PDF</a>
            <a href="#" class="btn" data-dismiss="modal">Close</a>

        </div>
    </div>

Script: 脚本:

<script>
        $('.btn-success').click(function(){
            var address = [];
            var trId = $(this).attr('id').split('_')[1];

            $('#row_'+trId).find('td').each (function() {
                address.push($(this).text());

           });
           alert(address.join('\n'));
           $('#myModal > .modal-body').html(address.join('\n'));
           $('#myModal').modal('show');
        });

    </script>

通过<textarea><i>string to Bootstrap Modal using Javascript</div></i><b>字符串到 Bootstrap Modal 使用 Javascript</div></b></textarea><div id="text_translate"><p> 学习 Javascript 并尝试学习如何传递值。 在此示例中,我有一个用户键入消息的位置,例如,该字符串存储并显示在 Bootstrap 模态中。 问题...除非您按回车键或空格键,否则最后一个字符会被截断。 我尝试在我的 document.getElementById(第 38 行)的末尾添加一个空格...尝试添加一个回车符和新行...我已经找到了几个非常简单的 Jquery 解决方案,但如果我能首先使用 JS 解决这个问题。 或者我应该将脚本切换为 JQuery? 我也知道我的字符数计算不正确,但我稍后会解决这个问题。</p><p> 谢谢! </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> //Write a function that shows how many characters are left in the textarea function charCount() { //Retrieve the stored value of the string from textarea var txt = document.getElementById("text-area").value; //Determine how many characters are in the textarea var chars = txt.length; //Subtract the max number of characters from the number of characters entered var maxLength = 140-chars; //Show how many characters remain in textarea document.getElementById("char-count").innerHTML = maxLength + " characters remaining"; //Write textarea string to modal document.getElementById("modal-body").innerHTML = txt + " "; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> <:-- Textarea --> <div class="row"> <div class="col-xl-12"> <textarea id="text-area" maxlength="140" onkeypress="charCount()"></textarea> </div> </div> <div class="row"> <div class="col-1"> <button class="btn btn-secondary d-inline" data-bs-toggle="modal" data-bs-target="#myModal">Submit</button> </div> <div class="col"> <div id="char-count"><!--140 Characters Remaining--></div> </div> </div> <div class="modal" id="myModal"> <div class="modal-dialog modal-dialog-centered"> <div class="modal-content"> <!-- Modal Header --> <div class="modal-header"> <h4 class="modal-title">Your Message:</h4> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <!-- Modal body --> <div class="modal-body" id="modal-body"> </div> <!-- Modal footer --> <div class="modal-footer"> <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button> </div> </div> </div> </div></pre></div></div><p></p></div> - Passing <textarea> string to Bootstrap Modal using Javascript

暂无
暂无

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

相关问题 通过<textarea><i>string to Bootstrap Modal using Javascript</div></i><b>字符串到 Bootstrap Modal 使用 Javascript</div></b></textarea><div id="text_translate"><p> 学习 Javascript 并尝试学习如何传递值。 在此示例中,我有一个用户键入消息的位置,例如,该字符串存储并显示在 Bootstrap 模态中。 问题...除非您按回车键或空格键,否则最后一个字符会被截断。 我尝试在我的 document.getElementById(第 38 行)的末尾添加一个空格...尝试添加一个回车符和新行...我已经找到了几个非常简单的 Jquery 解决方案,但如果我能首先使用 JS 解决这个问题。 或者我应该将脚本切换为 JQuery? 我也知道我的字符数计算不正确,但我稍后会解决这个问题。</p><p> 谢谢! </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> //Write a function that shows how many characters are left in the textarea function charCount() { //Retrieve the stored value of the string from textarea var txt = document.getElementById("text-area").value; //Determine how many characters are in the textarea var chars = txt.length; //Subtract the max number of characters from the number of characters entered var maxLength = 140-chars; //Show how many characters remain in textarea document.getElementById("char-count").innerHTML = maxLength + " characters remaining"; //Write textarea string to modal document.getElementById("modal-body").innerHTML = txt + " "; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> <:-- Textarea --> <div class="row"> <div class="col-xl-12"> <textarea id="text-area" maxlength="140" onkeypress="charCount()"></textarea> </div> </div> <div class="row"> <div class="col-1"> <button class="btn btn-secondary d-inline" data-bs-toggle="modal" data-bs-target="#myModal">Submit</button> </div> <div class="col"> <div id="char-count"><!--140 Characters Remaining--></div> </div> </div> <div class="modal" id="myModal"> <div class="modal-dialog modal-dialog-centered"> <div class="modal-content"> <!-- Modal Header --> <div class="modal-header"> <h4 class="modal-title">Your Message:</h4> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <!-- Modal body --> <div class="modal-body" id="modal-body"> </div> <!-- Modal footer --> <div class="modal-footer"> <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button> </div> </div> </div> </div></pre></div></div><p></p></div> - Passing <textarea> string to Bootstrap Modal using Javascript Bootstrap Modal - 使用Javascript将数据传递到Modal - Bootstrap Modal - Pass data into Modal using Javascript 将数据传递给模式(bootstrap) - passing data to modal(bootstrap) 自举模式传递数据 - Bootstrap modal passing data 将数据传递给引导模式 - Passing data to a bootstrap modal Bootstrap 将数据传递给模态 - Bootstrap Passing Data to a Modal 将数据传递给Bootstrap 3 Modal - Passing data to Bootstrap 3 Modal 如何在模态内单击按钮时使用jQuery或javascript获取引导模式的数据值? - How to get data values of a bootstrap modal using jQuery or javascript on clicking button inside the modal? jQuery 到 Javascript 用于引导模式 - jQuery to Javascript for bootstrap modal 将data-id传递给Bootstrap模式而无需使用jquery - Passing data-id to bootstrap modal WITHOUT jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM