简体   繁体   English

jQuery在表行克隆后的更改上动态更改文本框值

[英]Jquery change textbox values dynamically on Change after table row clone

I have a sort of complex php,html script that I am using to create an interactive design. 我有一种用于创建交互式设计的复杂的php,html脚本。 I am using a repeat table and i am basically cloning the contents of a particular table row and appending it to the end of the table. 我正在使用重复表,并且我基本上是在克隆特定表行的内容并将其附加到表的末尾。 When I select a certain option in the select menu, the corresponding textfields get updated with the correct values. 当我在选择菜单中选择某个选项时,相应的文本字段将使用正确的值进行更新。 However cloned rows misbehave 但是克隆的行行为不当

What I'm asking is if there is a way to make this fields also change values. 我要问的是是否有一种方法可以使此字段也更改值。 Here is the various code Code for adding/cloning a row. 这是用于添加/克隆行的各种代码。 Please note the id details is the id of a table row 请注意,ID详细信息是表格行的ID

$("#addrow1").click(function(){
        //alert("It works");

        //$('#description tr:last').after('<tr>...</tr>');
        $('#details').clone().appendTo('#dailyexpense');    


            });

Code for changing the textfield and textbox values 用于更改文本字段和文本框值的代码

$("#cities").live('change',function(){
var cityrates = $("#cities :selected").val();
var brkfstrates = (0.2 * cityrates).toFixed(1); 
$("#brkfasttxt").val(brkfstrates);
$("#brkfastchk").val(brkfstrates);
var lunchrates = (0.3 * cityrates).toFixed(1);
$("#lunchtxt").val(lunchrates);
$("#lunchchk").val(lunchrates);
var dinnerrates = (0.3 * cityrates).toFixed(1);
$("#dinnertxt").val(dinnerrates);
$("#dinnerchk").val(dinnerrates);
var incdntlrates = (0.2 * cityrates).toFixed(1);
$("#incidentltxt").val(incdntlrates);
$("#incidentlchk").val(incdntlrates);
});

My table row code for the one that gets loaded by the browser 我的表格行代码由浏览器加载

<tr id="details">
      <td><label for="date"></label>
      <input style="background-color:#CCC;" type="text" name="date" id="date" /></td>
      <td><label for="cities"></label>
        <select name="cities" id="cities" style="background-color:#CCC; width:220px;">
        <?php           
 foreach($country as $makassi1){
 $selectcities = "SELECT City, Country, Rate FROM perdiem_rates WHERE Country =   '$makassi1'";
 $checkcity = mysql_query($selectcities)or die(mysql_error());
 $countcities = mysql_num_rows($checkcity);

 while($row=mysql_fetch_assoc($checkcity))
 {

     $countries = ($row['Country']);
     $names =($row['City']) ;
     $rate =($row['Rate']) ;
     $ratess=$countries."-".$names
     ?> 

 <option id="cityoptrates"  value="<?php echo $rate; ?>"> 
 <?php echo $ratess; ?>
 </option>
 <?php       
 }
 }          
         ?>
      </select></td>
      <td><input name="brkfastchk" type="checkbox" class="chkbox"  id="brkfastchk" />
        <label for="brkfasttxt"></label>
        <input style="background-color:#CCC;" name="brkfasttxt" type="text" id="brkfasttxt" size="5" readonly="readonly" />
      <label for="brkfastchk"></label></td>
      <td><input type="checkbox" name="lunchchk"  id="lunchchk" class="chkbox" />
        <label for="lunchtxt"></label>
        <input style="background-color:#CCC;" name="lunchtxt" type="text" id="lunchtxt" size="5" readonly="readonly" />
      <label for="lunchchk"></label></td>
      <td><input type="checkbox" name="dinnerchk"  id="dinnerchk" class="chkbox" />
        <label for="dinnertxt"></label>
        <input style="background-color:#CCC;" name="dinnertxt" type="text" id="dinnertxt" size="5" readonly="readonly" />
      <label for="dinnerchk"></label></td>
      <td><input type="checkbox" name="incidentlchk"  id="incidentlchk" class="chkbox" />
        <label for="incidentltxt"></label>
        <input style="background-color:#CCC;" name="incidentltxt" type="text" id="incidentltxt" size="5" readonly="readonly" />
      <label for="incdntchk"></label></td>
      <td colspan="2"><label for="daily_totals"></label>
      <input style="background-color:#CCC;" name="daily_totals" type="text" id="daily_totals" size="5" /></td>
    </tr>

As per the above my textfield value changing code works perfectly with the first initial row. 按照以上所述,我的textfield值更改代码可与第一行初始完美配合。 The cloned rows don't work. 克隆的行不起作用。 The only one which works on the cloned rows is the select menu for selecting the cities. 唯一适用于克隆行的是用于选择城市的选择菜单。 Help needed. 需要帮助。 Open to suggestions and improvements. 欢迎提出建议和改进。

link to actual code in jsfiddle http://jsfiddle.net/NAafu/10/ 链接到jsfiddle中的实际代码http://jsfiddle.net/NAafu/10/

In your case maybe you should also clone the events: 就您而言,也许您还应该克隆事件:

 $('#details').clone(true).appendTo('#dailyexpense');   

as stated in the documentation of clone() clone()文档中所述

EDIT - The problem perhaps is that you are using an id selector ( $("#cities").live ) which returns only one element. 编辑-问题可能是您使用的是ID选择器( $("#cities").live ),该选择器仅返回一个元素。 Ids should be unique on the page you should use a class instead ID在页面上应该是唯一的,您应该改用类

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

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