【问题标题】:Send data-attributes to modal将数据属性发送到模态
【发布时间】:2014-07-24 13:29:18
【问题描述】:

我正在使用以下代码尝试将行数据从页面发送到模式窗口:

 <?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";
    }
   }
 ?>

正如您在上面的 a 标签中看到的,我使用 data-attributes 来获取行数据,其中包括 resgroup、restype、service 和 description。此时,我可以毫无问题地打开模态窗口。

我使用的 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>

我可以做一个警报($group),数据组的行数据确实出现在警报窗口中。

我的模态窗口有一个带有输入标签的表单,我试图用数据属性填充它。输入标签具有与数据属性本身同名的类、名称和 id。

我知道我不需要使用 console.log();但我不知道如何在警报窗口之外传递数据。

【问题讨论】:

    标签: javascript php jquery html


    【解决方案1】:

    看起来您已经在使用 jQuery,所以如果表单输入的 id 与数据属性名称匹配,我相信您应该可以将它们填写在 using the val() method 中:

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

    您也可以use the data() method 检索数据属性。而不是

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

    你可以使用

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

    【讨论】:

    • 在我的 javascript 中,我将在哪里应用 $('#group').val($group); ?
    猜你喜欢
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多