简体   繁体   English

带有类异常的克隆表行

[英]Cloning table row with a class exception

Using JS to clone a table row. 使用JS克隆表行。 The form elements are appended with a numeral as they are duplicated. 表单元素在重复时会附加一个数字。 I have that working fine. 我的工作很好。

My problem is I want to clear the values of most of the fields but those with a certain class I can't have the value cleared. 我的问题是我想清除大多数字段的值,但是对于某些类,我无法清除该值。

html html

<div class="add_rec_container" id="fuel_stop" style="display:none;"><!--open #fuel_stop-->
    <p class="label">Enter Fuel Stop:</p>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="fuel_stop_table" class="fuel_data">
      <tbody>
        <tr>
          <td><select name="data_fuel_state1" id="data_fuel_state1" class="state_menu">
            <option value="AZ">AZ</option>
            <option value="CA" selected="selected">CA</option>
            <option value="NM">NM</option>
            <option value="NV">NV</option>
            <option value="OR">OR</option>
            <option value="UT">UT</option>
            </select>
            </td>
          <td><input type="tel" name="data_total_gal1" id="data_total_gal1" class="total_gal_field" title="Gallons" placeholder="Gallons"/></td>
          <td><input type="checkbox" name="data_yard1" id="data_yard1" class="yard_check" value="y" onclick="disablePPG(this);"/> Yard
          </td>
          <td>$ <input type="tel" name="data_price1" id="data_price1" class="price_field" title="PPG" placeholder="PPG" /></td>
          </tr>
        </tbody>
    </table>
    <a id="add_fuel_row" class="add_btn">+ State</a>
    </div><!--close #fuel_stop-->

  <input type="submit" name="Submit" class="send_button" value="Send"/>
</form>​
</div><!--close .record_entry_container-->

js js

<!-- Inset Table Row --Fuel -->
$(document).ready(function($)
{
  // trigger event when button is clicked
  $("a#add_fuel_row").click(function()
  {
    // add new row to table using addTableRow function
    addTableRow($("table#fuel_stop_table"));

    // prevent redirecting
    return false;
  });

  // function to add a new row to a table by cloning the last row
  function addTableRow(table)
  {
    // clone the last row in the table
    var $tr = $(table).find("tbody tr:last").clone();

    // get the name attribute for input and select fields
    $tr.find("select, input").val('').attr("name", function()
    {
      // break the field name and it's number into two parts
      var parts = this.id.match(/(\D+)(\d+)$/);

      // create a unique name (+1 scheme for now)
      return parts[1] + ++parts[2];
    // repeat for id attributes
    }).attr("id", function()
    {
      var parts = this.id.match(/(\D+)(\d+)$/);
      return parts[1] + ++parts[2];
    });

    // append the new row to the table
    $(table).find("tbody tr:last").after($tr);
  };
});

If the input has a class of "yard_check" then I don't want to clear the value. 如果输入具有“ yard_check”类,那么我不想清除该值。 ie the check box needs to stay as value=y. 即该复选框需要保留为value = y。

Thanks. 谢谢。

For inputs/textareas, just use .val('') to clear the values of any given elements in a selector. 对于输入/文本区域,只需使用.val('')来清除选择器中任何给定元素的值。

$('selectorhere').val('');

For options, use 对于选项,请使用

$('select:first').attr('selected','selected');

For checkboxes, do the same except remove the checked property using .removeProp 对于复选框,执行相同的操作,只是使用.removeProp删除选中的属性

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

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