动态元素的添加和复制与方法参数
Dynamic Element Adding and Duplicating with method perameters
不好意思。我不知道如何解释这么具体的事情。
所以我一直在创建一个简单的表单,可以使用jQuery动态地删除和复制字段。工作好了!
问题 -我有一个函数,目前克隆last_input(参见JSFiddle例如)。这对于复制按钮非常有效,但对于添加按钮,我只想添加一个新的空白行。
我想知道是否可以很容易地添加一个额外的函数来解决这个问题,或者当我调用addLine()方法时,我可以使用不同的参数?
HTML:<form action="javascript:void(0);" method="POST" autocomplete="off">
<button class="add">Add Field</button>
<div class='input_line'>
<input type="text" name="input_0" placeholder="Input1">
<input type="button" class="duplicate" value="duplicate">
<input type="button" class="remove" value="remove">
</div>
</form>
<hr>
<form action="javascript:void(0);" method="POST" autocomplete="off">
<button class="add">Add Field</button>
<div class='input_line'>
<input type="text" name="input_0" placeholder="Input1">
<input type="button" class="duplicate" value="duplicate">
<input type="button" class="remove" value="remove">
</div>
</form>
JQUERY: function addLine($inputLine) {
var $form = $inputLine.closest('form');
var $newElement = $inputLine.clone(true).hide();
$newElement.insertAfter($inputLine);
$newElement.slideDown();
$form.find('.remove').show();
}
$(document).ready(function () {
'use strict';
$('.remove').hide();
//Add Feild
$('.add').click(function () {
addLine($(this).closest('form').find('.input_line:last'));
});
//Duplicate Feild
$('form').on('click', '.duplicate', function () {
addLine($(this).closest('.input_line'));
});
});
您可以像这样为addLine()
添加额外的参数
function addLine($inputLine, isDuplicate) {
var $form = $inputLine.closest('form');
var $newElement = $inputLine.clone(true).hide();
if(!isDuplicate) {
$newElement.find('input[type="text"]').val('');
}
$newElement.insertAfter($inputLine);
$newElement.slideDown();
$form.find('.remove').show();
}
addLine可以这样调用
$('.add').click(function() {
addLine($(this).closest('form').find('.input_line:last'), 0);
});
//Duplicate Feild
$('form').on('click', '.duplicate', function() {
addLine($(this).closest('.input_line'), 1);
});
这是更新后的演示http://jsfiddle.net/dhirajbodicherla/ge5r36pm/1/
相关文章:
- 如何确保函数/方法参数为certian类型
- 我怎么能读“;.on()"jQuery中的方法参数
- Javascript 方法参数转义
- 对象属性来自方法参数时未定义
- 在 c# 中将多维 JavaScript 数组绑定为方法参数
- 如何通过javascript获取post()方法参数
- AngularJS$http://.NET Web API-获取400(错误请求)或NULL POST操作方法参数
- _.chain()中的方法参数
- springmvc:如何在没有控制器方法参数的情况下以mvc传递参数
- JSON作为null传递给方法参数
- Javascript,可以在没有 eval 的情况下传递未声明的方法参数
- 单词“in”是什么意思?在文档中的方法参数列表中
- 如何返回对象方法参数
- 实例变量vs方法参数
- 如何传递Json字符串作为javascript方法参数
- 将方法参数/参数传递给方法引用绑定,匿名,命名
- 不合逻辑的Passport验证方法参数
- 调试javascript方法参数
- 如何使用javascript获取方法参数的值
- Javascript在字符串模板中插入方法参数