2011-10-19 28 views
1

我有一個jQuery對話框,當單擊「請確認」按鈕時發佈表單。jQuery-UI對話框丟失<button> POST數組中的名稱/值

我看到我的帖子的數據,但按鈕丟失其名稱/值: enter image description here

我怎樣才能得到它,以顯示該按鈕的名稱/值?期望的結果 enter image description here

這裏
例子是示例代碼testjQueryDialog.php演示此行爲:

<html><head> 
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" /> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    var $dialog = $('<div></div>') 
     .dialog({ 
      autoOpen: false, 
      title: 'Are you sure?', 
      modal: true, 
      closeOnEscape: true, 
      buttons: { 
       "Please confirm": function() { 
       $(this).dialog("close"); 
       $('#btnSubscription').parents('form').submit(); 
       }, 
       Cancel: function() { 
       $(this).dialog("close"); 
       } 
      } 
     }); 
    $('#btnSubscription').live('click', function() { 
     $dialog.dialog('open'); 
     return false; 
    }); 
}); 
</script> 
</head><body> 
<form action="/testjQueryDialog.php" method="post"> 
<div> 
    <input name="txtOne" type="text" value="One"> 
    <input name="txtTwo" type="text" value="Two"> 
    <button name="btnSubmit" id="btnSubscription" value="Subscription">Click</button> 
</div> 
<?php echo 'POST<br>'; print_r($_POST); ?> 
</form></body></html> 

回答

1

看起來好像jQueryUI對話框有點破壞,因爲它不返回按鈕名稱/值對。

我的解決方法是向窗體添加一個隱藏字段,並使用它傳遞按鈕值。

$('#btnSubscription').live('click', function() { 
    document.getElementById('hidSubmit').value = document.getElementById('btnSubscription').value 
    $dialog.dialog('open'); 
    return false; 
}); 

現在我需要的結果是這樣的: 陣列([txtOne] =>一[txtTwo] =>雙[hidSubmit] =>訂閱)

1

HTML4 spec規定:「如果窗體包含一個以上的提交按鈕時,僅激活提交按鈕成功。「

看起來像瀏覽器忽略了事實形式只有一個提交按鈕,並且由於您觸發了表單提交動態通過$('#btnSubscription').parents('form').submit(),按鈕不被視爲成功的控件。

問題駐留在按鈕click回調:

$('#btnSubscription').live('click', function() { 
    $dialog.dialog('open'); 
    return false; 
}); 

當回調返回false按鈕激活丟失,你需要的是返回基於用戶的選擇值:Returning value from confirmation dialog using JQuery UI dialog

+0

阿爾喬姆:感謝您的答覆。您提供的鏈接顯示使用傳播的方式,但不回答我的問題。我試過了,它給出了相同的結果:我可以在發佈數據中看到其他表單輸入值/對,但不是

+0

正確傳播的事件應該保留激活的按鈕值。如果您提供您嘗試的代碼,我可以嘗試檢查出現了什麼問題。 –