2013-02-10 92 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被更改,您的脚本就会中断。