2012-06-08 39 views
0

我有一個字段必須基於表單中的其他字段創建,所以我向提交事件處理程序添加了模態確認,但即使該值正確顯示在模式中,也不是張貼到處理程序。有什麼我必須做的特別插入一個值到$ _POST對象?jQuery沒有提交自定義值

這是我的提交功能:

$('form').submit(function(e){ 
    var occ = $('input:radio:[name=isDoubleOccupancy]:checked').val(), 
      numBeds = $('select[name=numBeds]').val(), 
      numBaths = $('select[name=numBaths]').val(), 
      fpImage = ''; 

    if(parseInt(occ) == 1){ 
     fpImage = fpImage + 'do'; 
    } else if(parseInt(occ) == 2){ 
     fpImage = fpImage + 'to'; 
    } else { 
     fpImage = fpImage + ''; 
    } // end if 

    fpImage = fpImage + numBeds + "X" + numBaths + ".png"; 
    $('input[name=fpImage]').val(fpImage); 

    if (!confirm("Please request floor plan graphics named " + fpImage + " from the development team.")) { 
     return false; 
    } // end if 
}); // end form submit 

一個例子POST的轉儲:

Array (
[id] => 
[fpName] => Efficiency 
[fpBullets] => Private bedroom with private bathroom; Approximately 1,399 square feet; Spacious walk-in closets 
[fpSubBullets] => 
[numBeds] => 1 
[numBaths] => 4 
[isDoubleOccupancy] => 0 
[fpRate] => 1600 
[payPeriod] => Monthly 
[numInstallments] => 12 
[fpSpecial] => 0 
[fpSpecialText] => 
[showHome] => 
[fpImage] => 
[fpSpecialURL] => 
[isSoldOut] => 
) 

你可以看到,[fpImage]被張貼NULL。我已經在谷歌等幾個小時尋找代碼似乎是正確的。 Firebug顯示沒有錯誤。我正式感到沮喪。

在此先感謝您的幫助。

這裏的HTML:

<div id="theEditor"> 
    <h2>EDIT FLOOR PLAN</h2> 

    <form action="http://www.edrpo.com/WEBdev/index.php/conn/create_ind_floor_plan" 
    method="post" accept-charset="utf-8"> 
     <label for="fpName">Floor Plan Name</label> <input type="text" name="fpName" value= 
     "" /><br /> 
     <br /> 
     <label for="fpBullets">Floor Plan Main Bullets</label> 
     <textarea name="fpBullets" cols="40" rows="10"> 
</textarea><br /> 
     <span class="emphasisTxt">The following will display as a bulleted list. To enter, 
     separate each bullet with a semicolon. Do not put a semicolon after the last 
     bullet.<br /> 
     DO NOT USE THE ENTER OR RETURN KEY!</span><br /> 
     <br /> 
     <label for="fpSubBullets">Floor Plan Optional Bullets</label> 
     <textarea name="fpSubBullets" cols="40" rows="10"> 
</textarea><br /> 
     <span class="emphasisTxt">The optional bullets will display as a bulleted list at 
     the bottom of the Floor Plans Info page. Use these to add temporary or suplemental 
     information such as seasonal rates, etc. To enter, separate each bullet with a 
     semicolon. Do not put a semicolon after the last bullet.<br /> 
     DO NOT USE THE ENTER OR RETURN KEY!</span><br /> 
     <br /> 
     <label for="numBeds">Number of Bedrooms</label> <select name="numBeds"> 
     <option value=""> 
      Please Select ... 
     </option> 

     <option value="0"> 
      Efficiency 
     </option> 

     <option value="1"> 
      One 
     </option> 

     <option value="2"> 
      Two 
     </option> 

     <option value="3"> 
      Three 
     </option> 

     <option value="4"> 
      Four 
     </option> 

     <option value="6"> 
      Dorm-style 
     </option> 
     </select><br /> 
     <br /> 
     <label for="numBaths">Number of Bathrooms</label> <select name="numBaths"> 
     <option value=""> 
      Please Select ... 
     </option> 

     <option value="1"> 
      One 
     </option> 

     <option value="2"> 
      Two 
     </option> 

     <option value="3"> 
      Three 
     </option> 

     <option value="4"> 
      Four 
     </option> 

     <option value="0"> 
      Dorm-style 
     </option> 
     </select><br /> 
     <br /> 
     <label for="isDoubleOccupancy">Unit Type</label> <input type="radio" name= 
     "isDoubleOccupancy" value="0" checked="checked" id="isDoubleOccupancy" style= 
     "margin:10px" />&nbsp;&nbsp;<span class='likeLabel'>Single-Occupancy</span> 
     <input type="radio" name="isDoubleOccupancy" value="1" id="isDoubleOccupancy" 
     style="margin:10px" />&nbsp;&nbsp;<span class='likeLabel'>Double-Occupancy</span> 
     <input type="radio" name="isDoubleOccupancy" value="2" id="isDoubleOccupancy" 
     style="margin:10px" />&nbsp;&nbsp;<span class= 
     'likeLabel'>Triple-Occupancy</span><br /> 
     <br /> 
     <label for="fpRate">Rate</label> <input type="text" name="fpRate" value="" style= 
     "width:80px;" /><br /> 
     <br /> 
     <label for="payPeriod">Pay Period</label> <select name="payPeriod"> 
     <option value=""> 
      Please Select ... 
     </option> 

     <option value="Monthly"> 
      Monthly 
     </option> 

     <option value="Semester"> 
      Semester 
     </option> 

     <option value="Biannual"> 
      Biannual 
     </option> 

     <option value="Annual"> 
      Annual 
     </option> 
     </select><br /> 
     <br /> 
     <label for="numInstallments">Number of Installments</label> <input type="text" 
     name="numInstallments" value="" style="width:80px;" /><br /> 
     <br /> 
     <input type="radio" name="fpSpecial" value="1" id="fpSpecial" style= 
     "margin:10px" />&nbsp;&nbsp;<span class='likeLabel'>Floor Plan Special ON</span> 
     <input type="radio" name="fpSpecial" value="0" checked="checked" id="fpSpecial" 
     style="margin:10px" />&nbsp;&nbsp;<span class='likeLabel'>Floor Plan Special 
     OFF</span><br /> 
     <br /> 
     <label for="fpSpecialText">Floor Plan Special Text</label> 
     <textarea name="fpSpecialText" cols="40" rows="10"> 
</textarea><br /> 
     <span class="emphasisTxt">Floor Plan Special Text will display on the Floor Plans 
     Info page for this floor plan.</span><br /> 
     <br /> 
     <label for="fpImage">Floor Plan Image</label> <input type="text" name="fpImage" 
     value="" disabled="disabled" style="width:80px;" /><br /> 
     <br /> 

     <p style='text-align:center;'><input type="image" name="submit" value="" src= 
     "http://www.edrpo.com/WEBdev/assets/images/update.png" /></p> 
    </form> 

    <p><br /></p> 
    </div><!-- END theEditor DIV --> 
+1

fpImage是什麼類型的輸入? –

+0

我們可以看到HTML代碼嗎? – Joey

+0

你可以發佈螢火蟲/谷歌瀏覽器的網絡截圖嗎? –

回答

1

你的HTML標記顯示重複的ID和其他問題。

<label for="isDoubleOccupancy">Unit Type</label> 

<input type="radio" name="isDoubleOccupancy" value="0" checked="checked" id="isDoubleOccupancy" style="margin:10px" />&nbsp;&nbsp; 

<span class='likeLabel'>Single-Occupancy</span> 

<input type="radio" name="isDoubleOccupancy" value="1" id="isDoubleOccupancy" style="margin:10px" />&nbsp;&nbsp; 

<span class='likeLabel'>Double-Occupancy</span> 

<input type="radio" name="isDoubleOccupancy" value="2" id="isDoubleOccupancy" style="margin:10px" />&nbsp;&nbsp; 

<span class='likeLabel'>Triple-Occupancy</span> 

可以肯定的是,任何ID只能使用一次。同上id="fpSpecial"和使用的任何其他ID。

編輯:每您郵件中我的註釋部分:

這也適用於您的name attributes

它們也都一樣,當你的函數設置變量爲var occ = $('input:radio:[name=isDoubleOccupancy]:checked').val()時,它將無法正常工作,因爲該值始終爲0

因爲它總是0,自定義值將永遠提交。我的回答描述了爲什麼你有這個問題,因爲fpImage = fpImage + '';將永遠是這種情況,因此沒有圖像。

解決方案:使用唯一的idname屬性並相應地修改您的腳本。

+0

我正在使用codeigniter,並且這些無線電正在有條件地確定它是否是更新頁面或創建頁面。我沒有意識到這個代碼重複了這個id。我不需要ID,所以我會擺脫所有這些以防止重複。 – jgravois

+0

請參閱本答覆的答覆編輯,謝謝。 – arttronics

0

你需要一個domready中的功能,使所有的元素和變化,可以發現:

$(function() {//domready 
    $('#form').submit(function() { 
        // DO STUFF 
        return true; // return false to cancel form action 
    }); 
}); 
+0

我確實有一個。我剛剛粘貼了自調用匿名domready函數的提交功能。 – jgravois