【问题标题】:jquery dialog widget not passing my form valuejquery 对话框小部件没有传递我的表单值
【发布时间】:2015-07-27 20:52:05
【问题描述】:

我有如下图所示的表格,我在其中从查询创建到数据库,每行都有按钮,onclick 将出现提示对话框(我使用 jquery 对话框小部件)

这个 myform html 代码

<form action="prodi_tampil_mhs.php" method="post" name="form_tambah_cl_wisudawan" id="form_tambah_cl_wisudawan">

这是我从查询数据库创建表的 php 代码

while ($fetch_dbsi_mhsw=mysql_fetch_array($query_dbsi_mhsw)) {
$no++;
echo" <tr>
<td>$no</td>
<td>$fetch_dbsi_mhsw[NIM]</td>
<td>$fetch_dbsi_mhsw[Name]</td>
<td style=\"text-align: center;\"><input name=\"bt_tambah_calon_wisudawan\" id=\"bt_tambah_calon_wisudawan\" type=\"image\" src=\"buttonTambah.png\" alt=\"Tambah\" align=\"middle\" width=\"20\" height=\"20\" class=\"bt_tambah_calon_wisudawan\"   /></td></tr>";}

还有我的 jquery 代码

$(document).ready(function(){
    $(".bt_tambah_calon_wisudawan").click(function(){
        var value1 = $(this).closest('tr').find('td:eq(1)').text();
        var value2 = $(this).closest('tr').find('td:eq(2)').text();
        // Here's the text of the dialog box 
        var dialog = $("<div style='display: none'><p>Anda akan menambahkan "+value1 + " " + value2 + " sebagai calon wisudawan?</p></div>").appendTo("body");
        // This is the button on the form
        var form = $("#form_tambah_cl_wisudawan")
        // The form button was pressed - open the dialog
        $(dialog).dialog({
            title: "Konvirmasi Penambahan Data",
            dialogClass: "no-close",
            width: 600,
            modal: true,
            buttons: {
                "Tambah": function () {
                     // This will invoke the form's action - putatively deleting the resources on the server
                     $(form).submit();
                     $(this).dialog("close");
                },
                "Cancel": function() {
                    // Don't invoke the action, just close the dialog
                    $(this).dialog("close");
                }
            }
        });
        return false;
    });
 });

我的对话框出现(如图 2)

当我单击对话框中的按钮“tambah”时,表单中的值未通过。我的 jquery 代码有什么错误吗?有什么帮助吗?

【问题讨论】:

  • value1value2 没有做任何事情,最简单的解决方案是在表单中添加 2 个隐藏字段然后设置值
  • 我在表单中添加了 2 个隐藏字段但无法正常工作,有没有办法从 jquery 代码中传递值?因为提交按钮如何知道要传递我的行值中的哪一个?
  • 设置值如$("#hiddenFieldIdForValue1").val(value1)
  • 我在我的 php cde 和 jquery 代码中添加了 hiddenFieldIdForValue1 和 2,但没有任何反应,然后我将表单方法更改为“get”,我在我的 url 中看到了这样的 prodi_tampil_mhs.php?hiddenFieldIdForValue1 =A111002&hiddenFieldIdForValue2=la+i‌​min+lagi%0D%0A++++&hiddenFieldIdForValue1=A111002&hiddenFieldIdForValue2=la+imin+‌​lagi&hiddenFieldIdForValue1=A111003&hiddenFieldIdForValue2=la+imin+lagilagi,所以按钮传递了表中的所有值。我只想要我点击按钮所在行的值。

标签: javascript php jquery html


【解决方案1】:

$(form).submit(); 替换为form.submit();,因为您已经将选择器$("#form_tambah_cl_wisudawan") 存储到var form

【讨论】:

  • 谢谢,我已经编辑了我的代码,还添加了 Satpal sugest,但还没有工作。
【解决方案2】:

如果您只是通过id selector 和submit() form 引用表单,那么操作将按预期进行。

JS 代码:

 $("#dialog-confirm").dialog({
    resizable: false,
    modal: true,
    title: "Modal",
    height: 250,
    width: 400,
    buttons: {
        "Yes": function () {
            $('#form_tambah_cl_wisudawan').submit();
            $(this).dialog('close');
        },
         "No": function () {
            $(this).dialog('close');
            callback(false);
        }
      }
  });

现场演示@JSFiddle:http://jsfiddle.net/vvjj8/7506/

【讨论】:

    猜你喜欢
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多