在复选框上添加动态行和数据单击

Adding Dynamic Row and Data on Checkbox Click

本文关键字:数据 单击 动态 复选框 添加      更新时间:2023-09-26

我又遇到了一个jQuery问题,希望得到一些帮助。

我有一系列像这样的复选框:

<input type="checkbox" name="MultiPointInspection" id="MultiPointInspection" class="MultiPointInspection" value="<?php echo $MultiPointInspection; ?>" onKeyPress="return disableEnterKey(event)" rel="Multi Point Vehicle Inspection" />&nbsp;Multi Point Vehicle Inspection<br />
<input type="checkbox" name="TireRotationandBrake" id="TireRotationandBrake" class="TireRotationandBrake" value="<?php echo $TireRotationandBrake; ?>" onKeyPress="return disableEnterKey(event)" />&nbsp;Tire Rotation and Brake Inspection<br />

我想要做的是添加一个新的动态行到前一个表(我已经可以做一个单独的按钮),但在这种情况下,添加名称,或id或类名(这都是相同的每上面)作为一个文本框值在该行当这些被点击。我还希望它们在取消点击时被删除,但它们并不总是最后一行,所以:last并不适合我。

这是前一个表:

<table id="atable" class="atable">
<tr>
<td>Description</td><td>Stocked</td><td>Quantity</td><td>Part Price</td><td>Hours</td><td>Rate Class</td><td>Total</td><td>Approved</td><td>Remove Row</td></tr>
<tr class="dynamic_row">
<td><input name="description[]" id="description" value="<?php echo $description; ?>" size="55" class="numeric add_to_total description" onKeyPress="return disableEnterKey(event)" />
</td><td align="center"><input type="checkbox"  name="stocked[]" id="stocked" value="<?php echo $stocked; ?>" size="5" class="numeric add_to_total" onKeyPress="return disableEnterKey(event)" />
</td><td><input name="quantity[]" id="quantity" value="<?php echo $quantity; ?>" size="5" class="numeric add_to_total quantity" onKeyPress="return disableEnterKey(event)" />
</td><td><input name="partPrice[]" id="partPrice" value="<?php echo $partPrice; ?>" size="10" class="numeric add_to_total part" onKeyPress="return disableEnterKey(event)" />
</td><td><input name="hours[]" id="hours" value="<?php echo $hours; ?>" size="10" class="numeric add_to_total hours" onKeyPress="return disableEnterKey(event)" />
</td><td><select name="rate[]" id="rate" class="numeric add_to_total rate" onKeyPress="return disableEnterKey(event)">
<?php
    //get rate options from database
?>
</select>
</td><td><input name="total[]" id="total" size="10" class="numeric is_total" readonly="readonly" onKeyPress="return disableEnterKey(event)" />
</td><td align="center"><input type="checkbox" name="approved[]" id="approved" class="add_to_total approved" checked="checked" value="<?php echo $approved; ?>" size="10" onKeyPress="return disableEnterKey(event)" />
</td><td align="center"><img src="img/x.png" width="25" height="25" id="removeButton" title="Remove Row" class="delRow" />
</td></tr>
<img src="img/plus.png" width="25" height="25" id="btnAddRow" title="Add New Row" class="alternativeRow" />    
</table>

我正在使用jQuery表AddRow插件。我使用以下代码使其添加行,这很好(虽然我没有得到我漂亮的交替行),但它也将行添加到底部,在我的"添加行"按钮下方,所以我也在寻找appendTo()的解决方案,并且宁愿将其添加在最后一行之上:

$("#MultiPointInspection,#TireRotationandBrake").on('click', function() {
    if ($(this).prop("checked")) {
        $('#atable .dynamic_row:first').clone().appendTo("#atable");
    }
    else {
        $('#atable .dynamic_row:last').remove();
    }
});

这是在另一个按钮上添加一行的常规代码,点击:

$("document").ready(function(){
    $(".alternativeRow").btnAddRow({oddRowCSS:"oddRow",evenRowCSS:"evenRow"});
    $(".delRow").btnDelRow();
});

好像我的要求还不够,我还想做的是不为这些行添加最后一列。最后一列包含一个用于删除该行的链接,并且我只希望在取消单击复选框时删除该行。

我已经创建了一个问题的基本设置(注意javascript包含完整的jQuery表AddRow插件代码),并将感谢任何帮助,我可以得到这个工作。

谢谢。

我建议使用change代替。试试

$("#MultiPointInspection,#TireRotationandBrake").on('change', function() {
    if ($(this).is(':checked')) {
        $('<tr><td><input type="text" id="'+$(this).attr("id")+'" 
              class="'+$(this).attr("class")+'" name="'+$(this).attr("name")+'"/>
                     </td></tr>').appendTo("#atable");
    }
    else {
        $('input#'+$(this).attr("id")+'[type="text"]').remove();
    }
});

小提琴:http://jsfiddle.net/dz2PR/1/