简体   繁体   English

发送数据属性到模式

[英]Send data-attributes to modal

I am using the following code in an attempt to send row data from the page to a modal window: 我正在使用以下代码,尝试将行数据从页面发送到模式窗口:

 <?php
   $select = "SELECT * FROM table";
   $res = mysql_query($select) or die();
     echo "<div>"
     echo "<table>"
     echo "<tr><th>Edit/Delete</th>
               <th>Group</th>
               <th>Type</th>
               <th>Service</th>
               <th>Description</th>
           </tr>";
     while(($Row = mysql_fetch_assoc($res)) !== FALSE){
       echo "<tr><td>
       <a href='' class='open-EditRow btn btn-primary' value='Edit'
         data-des=\"{$Row[description]}\" 
         data-group=\"{$Row[resgroup]}\" 
         data-type=\"{$Row[restype]}\" 
         data-service=\"{$Row[service]}\">Edit</a>
       </td>";
       echo "<td>{$Row[resgroup]}</td>";
       echo "<td>{$Row[restype]}</td>";
       echo "<td>{$Row[service]}</td>";
       echo "<td>{$Row[description]}</td></tr>\n";
       };
     echo "</table>";
     echo "</div>";
    if(mysql_num_rows($res) == 0){
      echo "No Results";
    }
   }
 ?>

As you can see in the a tag above, I am using the data-attributes to get the row data, which includes resgroup, restype, service, and description. 如您在上面的标签中所见,我正在使用数据属性来获取行数据,其中包括resgroup,restype,service和description。 At this point, I can get the modal window to open with no problem. 在这一点上,我可以打开模式窗口而没有问题。

The javascript I am using looks like this: 我正在使用的javascript看起来像这样:

 <script type="text/javascript">
 $(function()
   {
     $('.open-EditRow').click(function(e){
     e.preventDefault();
     $group = $(this).attr('data-group');
     $type = $(this).attr('data-type');
     $service = $(this).attr('data-service');
     $descript = $(this).attr('data-description');
     console.log($group);
     console.log($type);
     console.log($service);
     console.log($descript);
    });
   });
 </script>

I can do an alert($group) and the row data for data-group does indeed appear in the alert window. 我可以做一个alert($ group),而data-group的行数据确实出现在警报窗口中。

My modal window has a form with input tags that I am trying to populate with the data-attributes. 我的模态窗口有一个带有输入标签的表单,我试图用数据属性填充该表单。 The input tags have the a class, name, and id with the same name as the data-attributes themselves. 输入标签具有与数据属性本身同名的类,名称和ID。

I know I do not need to use console.log(); 我知道我不需要使用console.log(); but I am not sure how to pass the data any further than an alert window. 但是我不确定如何将数据传递到警报窗口之外。

It looks like you are already using jQuery, so if the form inputs have ids that match the data-attribute names, I believe you should be able to fill them in using the val() method : 看起来您已经在使用jQuery,因此,如果表单输入的ID与数据属性名称匹配,我相信您应该可以使用val()方法来填充它们:

$('#group').val($group);

You may also use the data() method to retrieve the data attributes. 您也可以使用data()方法检索数据属性。 Instead of 代替

$group = $(this).attr('data-group');

you can use 您可以使用

$group = $(this).data('group');

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

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