douwu7168 2011-01-05 18:25
浏览 15
已采纳

调整JavaScript计数

I'll try to explain this logically as best as I can. Basically I have a facility on my site whereby users can enter their employment history. This is a form with input fields. To make things easier I have a jQuery function that clones these input fields and allows the user to enter multiple records at once.

Whilst adding new records the user may wish to remove a record from the list, in which case they can press the 'remove' link next to each record.

HTML (1 Item):

<div class="history-form-fields">

<div class="row">
  <label for="History_0_type">Type</label>
  <select name="History[0][type]" id="History_0_type">
  <option value="">Select</option>
  </select>
</div>

<div class="row">
  <label for="History_0_name">Name</label>
  <input type="text" name="History[0][name]" id="History_0_name" />
</div>

<div class="row">
  <label for="History_0_year">Year</label>
  <select name="History[0][year]" id="History_0_year">
  <option value="">Select</option>
  </select>
</div>

</div>

<input id="add" type="button" value="Add Another" />    

JS:

<script type="text/javascript">
    var counter = <?php echo $historyCount; ?>;

    $('#add').click(function(){
        var divCloned = $('.history-form-fields:first').clone();
        divCloned.insertAfter('.history-form-fields:last');
        initNewInputs(divCloned.children('.row'), ++counter);
    });

    $('.remove').live('click', function(){
        $(this).parent().remove();
        return false;
    });
</script>

In the above JS code, the $historyCount variable is set beforehand in my PHP script:

$historyCount=isset($_POST['History']) ? count($_POST['History'])-1 : 0;

So for example when the form is posted, if there were multiple records on the page and there are some validation errors, then the form is redisplayed showing those same records.

There is also a call to a function initNewInputs() which is passed in the new counter value. This is so that new form input elements can receive unique ID and NAME attributes (in the HTML code above you can see the integer indexes within the attribute values).

This is all working fine. The issue I'm having is in the following scenario:

  1. User starts adding in new records

    • Record 1 - Count 0
    • Record 2 - Count 1
    • Record 3 - Count 2
    • Record 4 - Count 3
    • Record 5 - Count 4

  2. User removes Records 3 & 4 (so we now only have 3 records). Record 5 is now Record 3 but maintains the same Count value (4).

  3. User then adds in a new record:

    • Record 4 - Count 5

  4. User posts the form - there are some validation errors so the form is redisplayed. The PHP script determines that it received 4 records, so it pre-sets the count to 3.

  5. User decides to add a new record whilst fixing the errors

    • Record 5 - Count 4

This is where the problem arises - a record with the count value of 4 already exists on the page. I.e. it has now created input elements with the same ID and NAME attribute values as one that is already on the page.

I know this is a little bit difficult to understand but I would appreciate any help. It will most likely require modification of how the counter is set.

  • 写回答

1条回答 默认 最新

  • doumin1897 2011-01-05 18:37
    关注

    Rather than using count($_POST['History']) to fill in counter use the upper bound (in this case, I mean highest index number) of the $_POST['History'] array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用