【问题标题】:use jquery dialog button to submit form使用 jquery 对话框按钮提交表单
【发布时间】:2012-05-04 00:44:28
【问题描述】:

从 jQuery 对话框处理提交功能(基于this 问题)。使用 codeigniter 和 jquery。

我想将 ajax POST 绑定到 jquery 对话框按钮单击,但不知道该按钮的选择器应该是什么。我尝试使用 .ui-dialog-buttonpane button:first 或 "this" 作为选择器;都没有用。

HTML 表单

<?php echo form_open('bookmarks/addBookmark'); ?> //form name: create_bookmark
<?php echo form_input('bookn', $bname); ?>
<?php echo form_hidden('booki', $this->uri->segment(4, 0)); ?>
<button class="bb_button">Bookmark</button>
<?php echo form_close(); ?>

jQuery

$(function() {

        $( "#create_bookmark" ).dialog({
            autoOpen: false,
            width: 250,
            height: 250,
            modal: true,
            buttons: {
                "Add location": function() {    

                $('?????').click(function() {  //How do I specify the dialog button?

            $.ajax({
                url: '/bookmarks/addBookmark',
                type: 'POST',
                data:{"bookn": $("input[name='bookn']").val()​, "booki": $("[name='booki']").val()​},
                success: function (result) {
                    alert("Your bookmark has been added.");
                }                                         
            }); 
                },
                Cancel: function() {$( this ).dialog( "close" );}
            }
        });
    });

【问题讨论】:

    标签: jquery jquery-ui codeigniter codeigniter-2


    【解决方案1】:

    您在buttons 对象中指定的函数点击处理程序。
    您可以直接在该函数中发送请求。

    【讨论】:

    • 我删除了我添加的(重复)单击功能并获得了成功对话框,但没有任何内容进入数据库。将继续检查。
    • 不,它不起作用。我知道 ajax 代码很好,因为我在应用程序的其他地方使用它。
    • @chowwy:你在 Firebug 中看到了什么?
    • 谢谢;接受并赞成。我现在收到成功消息,但没有任何内容进入数据库。不知道为什么。
    【解决方案2】:

    看起来在我把它拆开并正确缩进之后,你错过了} 这是假设 Slaks 答案的实现

            buttons: {
              "Add location": function() {    
    
                $.ajax({
                  url: '/bookmarks/addBookmark',
                  type: 'POST',
                  data:{
                    "bookn": $("input[name='bookn']").val()​, 
                    "booki": $("[name='booki']").val()
                  ​},
                  success: function (result) {
                    alert("Your bookmark has been added.");
                  }
              });                                       
            } //<<This one  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多