繁体   English   中英

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

[英]Adding Dynamic Row and Data on Checkbox Click

我再一次遇到了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 Table AddRow插件 我使用下面的代码添加行,这很好(尽管我没有得到漂亮的交替行),但是它也将行添加到底部的“添加行”按钮下面,所以我m还在寻找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 Table 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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM