2013-02-10 53 views
1

我目前居住的問題是需要將值傳遞給Jquery UI對話框,但我不知道如何。我已到處檢查文檔,但似乎沒有任何工作。這是我有什麼(都試過)如何在JQuery UI對話框中設置元素的值

實際對話:

<div id="amountDialog" title="Add to Basket"> 
     <table style="text-align: center"> 
      <tr> 
       <td colspan="2"> 
        <p id="productAdd"></p> <!-- need to set this value --> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="text" id="modalInputAmount" /> 
       </td> 
       <td> 
        <p id="maxAmt">Maximum Amount: </p> 
       </td> 
      </tr> 
     </table> 
    </div> 

對話框選項:

$("#amountDialog").dialog({ 
         autoOpen: false, 
         width: 'auto', 
         modal: true, 
         buttons : { 
          "OK" : execute 
         }, 
         open : function (event, ui) { 
          $("#productAdd").val('How many [' + item + ']\'s do you wish to add to the basket?"'); <!-- item is the global variable describing which item is added to the basket --> 
         } 
        }); 

開放代碼

$("#amountDialog").dialog("open"); 

我不會打擾粘貼在這裏執行代碼,因爲我知道這是行得通的,我可以從模式對話框中的文本框中獲取值。然而,我擔心的是,如何在模態對話框打開時設置#productAdd段落的值?

只需要把它放在那裏,我仍然是一個JQuery的新手。

+0

我認爲它返回了一個值。打開對話框的按鈕嵌套在table> tbody> tr> td(它是一個動態按鈕)之間。所以這(按鈕)。父母(td)。父母(tr)。第n個孩子(1)因此獲得該行中第一列的值,該按鈕在 – Eon 2013-02-10 08:14:23

+0

中或者不是。只是看到了錯誤哈哈 – Eon 2013-02-10 08:14:43

回答

4

應該是html()val(),變化:

$("#productAdd").val(...) 

$("#productAdd").html(...); 
+0

這工作。謝謝。將在幾分鐘內設置爲正確的答案 – Eon 2013-02-10 08:10:11

+0

不客氣.. :) – 2013-02-10 08:10:40

+0

但是,如何知道何時使用val以及何時使用html? 從我看到的所有代碼片段中,這開始混淆了我 – Eon 2013-02-10 08:11:19

0

這裏你的問題是:#productAdd是一個段落(<p>)。 jQuery的val()方法只適用於表單域。改爲使用htmltext。例如。 $('#productAdd').text("How many ...")

在附註中,您想重新考慮您如何確定#productAdd的新內容。您現在有了parent().parent().find('specific thing')意味着只要HTML被更改,您的腳本就會中斷。

相關問題