在复选框上添加动态行和数据单击 [英] Adding Dynamic Row and Data on Checkbox Click

查看:109
本文介绍了在复选框上添加动态行和数据单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再一次遇到jQuery问题,我希望得到一些帮助。

I am once again stuck with a jQuery issue, and I'm hoping for some help.

我有一系列复选框看起来像这两个:

I have a series of checkboxes that look like these two:

<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不适用于我。

What I want to do is add a new dynamic row to a previous table (which I can already do with a separate button), but in this case, add the name, or id or class name (which are all the same as per above) as a textbox value in that row when any of these are clicked. I also want them to remove when they are unclicked, but they won't always be the last row, so :last doesn't really work for me.

这是上表:

<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插件。我已经使用下面的代码来添加行,这很好(虽然我没有得到我漂亮的交替行),但它也将行添加到底部,在我的添加行按钮下面,所以我'我也在寻找appendTo()的解决方案,而宁愿将它添加到最后一行的上方:

I am using the jQuery Table AddRow plugin. I have used the following code to make it add the row, which is fine (although I don't get my pretty alternating rows), but it also adds the row to the bottom, below my "add row" button, so I'm also looking for a solution to the appendTo(), and would rather have it added just above the last row:

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

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

and this is the regular code to add a row on the other button click:

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

好像我的要求不够,我还想做的就是没有最后一个为这些行添加了列。最后一列包含一个删除行的链接,我只想在取消选中该复选框时删除该行。

As if I'm not asking enough, what I would also like to do is not have the last column added for these rows. The last column contains a link to remove the row, and I would only like the row to be removed on the unclick of the checkbox.

我创建了一个小提琴与问题的基本设置(注意javascript包含完整的jQuery表AddRow插件代码),并会很感激我可以通过这项工作获得任何帮助。

I have created a fiddle with the basic setup of the problem (note the javascript contains the full jQuery Table AddRow plugin code), and would appreciate any help I could get with making this work.

谢谢。

推荐答案

我建议改用更改。尝试类似

$("#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/

这篇关于在复选框上添加动态行和数据单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆