将变量传递给jQuery UI对话框

时间:2011-06-13 13:32:01

标签: javascript jquery jquery-ui

我是jQuery的新手,并且很难将变量传递给jQuery UI对话框。单击“是”时,浏览器应重定向到localAuthorityDelete并删除相应的记录。

<script type="text/javascript">
   $(document).ready(function(){
       $('#deleteButton').click(function(){
           $('#deleteText').dialog({
            modal:true,
            resizable: false,
            buttons: {
                'Yes': function() {
                    window.location = "../php/localAuthorityDelete.php?laID="
                    $(this).dialog('close');
                },
                Cancel: function() {
                    $(this).dialog('close');
                }
            },                                 
            });   
       });
   });
</script>

我有很多删除按钮:

<input type="button" id="deleteButton" name="deleteButton" value="Delete">

每个删除按钮都在记录旁边的表格中,应该删除。我是这样做的:

<a href="localAuthorityDelete.php?laID=<?php echo $row_laID['laID'];?>">Delete</a>

但需要利用jQuerys对话框。如果有人可以帮助我,我会非常感激。我已经尝试将上面的JS包装在一个函数中,该函数接受一个变量,但它不会运行。

感谢您的帮助。

麦克

1 个答案:

答案 0 :(得分:1)

看起来你有多个deleteButtons,但html元素的id应该是唯一的。 因此,将删除按钮的呈现方式更改为:

<input type="button" 
       id="delete-<?php echo $row_laID['laID'];?>" 
       class="deleteButton" 
       name="deleteButton" 
      value="Delete"
/>

现在,您可以从删除按钮的ID中获取要删除的元素的ID。 点击处理程序现在应该绑定到.deleteButton而不是#deleteButton。

<script type="text/javascript">
     $(document).ready(function(){
         $('.deleteButton').click(function(){
            var id = this.id.replace(/[^0-9]/g, "");
            $('#deleteText').dialog({
                modal:true,
                resizable: false,
                buttons: {
                    'Yes': function() {
                        window.location = "../php/localAuthorityDelete.php?laID=" + id;
                        $(this).dialog('close');
                    },
                    Cancel: function() {
                        $(this).dialog('close');
                    }
                }
            });   
         });
     });
</script>
相关问题