jQuery没有提交自定义值

时间:2012-06-08 21:35:47

标签: jquery

我有一个必须根据表单中的其他字段创建的字段,所以我向submit事件处理程序添加了一个模态确认,但即使该值正确显示在模态中,它也不会发布到处理程序。在$ _POST对象中插入一个值是否需要做些什么?

这是我的提交功能:

$('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。我已经在Google上等了好几个小时,而且代码似乎正确。 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 -->

2 个答案:

答案 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属性并相应地修改您的脚本。

答案 1 :(得分:0)

您需要一个domready函数,以便找到所有元素和更改:

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