2011-05-19 43 views
0

我正在使用Coldfusion MX7,並具有可以將幾個元素動態添加到表單的基本表單。它們被賦予相同的名稱,並且都是複選框。形式的例子如下:使用CF無法訪問的表單元素MX7

<form action="index.cfm?action=index.report" method="post" id="reportForm"> 
<div class="report my"> 
    <ul class="connectWith ui-sortable" id="fieldListSelect" aria-disabled="false"> 
     <li class="field" id="field_profileFn" style=""> 
      <a class="action" id="action_profileFn" href="index.cfm?action=index.filter.profileFn" style="display: block; ">filter</a> 
      <label for="profileFn">First Name</label> 
      <input type="checkbox" name="reportItem" id="profileFn" value="profileFn"> 
     </li> 
     <li class="field" id="field_profileSn" style=""> 
      <a class="action" id="action_profileSn" href="index.cfm?action=index.filter.profileSn" style="display: block; ">filter</a> 
      <label for="profileSn">Surname</label> 
      <input type="checkbox" name="reportItem" id="profileSn" value="profileSn"> 
     </li> 
     <li class="field" id="field_contactDate" style=""> 
      <a class="action" id="action_contactDate" href="index.cfm?action=index.filter.contactDate" style="display: block; ">filter</a> 
      <label for="contactDate">Contact date</label> 
      <input type="checkbox" name="reportItem" id="contactDate" value="contactDate"> 
     </li> 
    </ul> 
</div> 
</form> 

一旦表單被提交我打通cfdump如下:

<table class="cfdump_struct"> 
    <tr><th class="struct" colspan="2" onClick="cfdump_toggleTable(this);" style="cursor:hand;" title="click to collapse">struct</th></tr> 

     <tr><td class="struct" onClick="cfdump_toggleRow(this);" style="cursor:hand;" title="click to collapse">CONTACTDATE_FROM</td> 
     <td> Thu May 19 2011 00:00:00 GMT+0100 (GMT Daylight Time) </td></tr> 
     <tr><td class="struct" onClick="cfdump_toggleRow(this);" style="cursor:hand;" title="click to collapse">CONTACTDATE_TO</td> 
     <td> Thu May 19 2011 00:00:00 GMT+0100 (GMT Daylight Time) </td></tr> 
     <tr><td class="struct" onClick="cfdump_toggleRow(this);" style="cursor:hand;" title="click to collapse">FIELDNAMES</td> 
     <td> REPORTITEM[],CONTACTDATE_FROM,CONTACTDATE_TO </td></tr> 
     <tr><td class="struct" onClick="cfdump_toggleRow(this);" style="cursor:hand;" title="click to collapse">REPORTITEM[]</td> 
     <td> profileFn,profileSn,contactDate </td></tr> 
    </table> 

元素REPORTITEM []報道,並試圖訪問這個作爲可變我得到:

<cfset testing = form.reportItem[]> 

Invalid CFML construct found on line 6 at column 50. 

在試圖訪問的方式有變我希望我得到如下:

<cfset testing = form.reportItem> 

Element REPORTITEM is undefined in FORM. 

我已經繼承了這個代碼,它必須以前有效。 Coldfusion尚未升級(顯然仍然是CF 7),沒有其他任何改變了我能想到的服務器端。

我的問題:

  • 這僅僅是一個CF7的限制嗎?
  • 這應該是正確的還是完全錯誤?
  • 如果這不起作用,我將不得不重寫這段代碼,在數據發佈後處理這些代碼會更容易編寫代碼。修改表單會更加努力,所以可能嗎?

回答

3

嘗試做

<cfset testing = form["reportItem[]"]> 

這將通過密鑰 「reportItem []」 獲取的形式結構。

+0

終於在星期五失蹤後回到工作,因爲我的女兒有水痘,並能夠測試。工作絕對的對待。 – bjammin666 2011-05-23 08:46:47

0

據我所知,CF7在這方面沒有問題。事實上,我很確定您的複選框的價值是由瀏覽器構建的,而不是Web服務器或CF.

這是我看到:

form.variableNamve[] 

將無法​​正常工作,因爲該值回來用逗號分隔的列表。

如果沒有選中複選框,您將遇到未定義的錯誤,因爲如果沒有選中具有該名稱的複選框,那麼該變量將不會被瀏覽器提交,因此不會在表單範圍中存在。你應該默認這個,並且有幾種方法可以做到這一點。

您可以創建一個新的結構,其中複選框名稱爲鍵,空字符串作爲值,然後structAppend將表單範圍置於其上。

您可以使用傳統的cfparam標籤。

您可以將具有相同名稱和空字符串的隱藏表單字段添加爲表單的值。這會強制瀏覽器返回表單域,即使沒有選中複選框。

HTH。

0

你是通過jQuery的ajax發佈或使用正常的提交按鈕。我認爲jQuery添加變量名[]時發佈它,但有辦法禁用它。但是在提交按鈕的情況下,只有在至少一個複選框被選中的情況下,我纔會在表單結構中獲得複選框。 在這種情況下總是使用默認值cfparam複選框名稱。

+1

儘管正在使用Jquery,但它並未涉及此問題。 – bjammin666 2011-05-24 11:27:47