jQuery,在toggle()中重置表单值
jQuery, resetting form value in a toggle()
我使用JQuery toggle()函数允许用户在表单中动态地向选择框添加新值。
如果用户单击"添加",则会显示一个DIV,其中包含一个输入框,并且"添加"文本将更改为"删除"。
如果用户单击"添加",然后在新的输入框中输入一些文本,然后单击"删除",我想清除输入框。
这里的一切都有效,但我不清楚在用户切换"删除"的情况下,如何重置新创建的输入框的表单值
这是我的代码
$(".newName").click(function(){
var frm = $(this).closest("form");
$(".newContainer", frm).toggle(); // toggles the newContainer to show or hide
$(this).text($(this).text() == 'Add' ? 'Remove' : 'Add'); // changes our text
$("#nameList", frm).prop('disabled', $(this).text() != 'Add') // disables the original list if we are adding a new one
});
<div class="names">
<form id="newForm">
<select id="nameList"></select>
<p><a href="#" class="newName">Add</a></p>
<div class="newContainer">
<input type="text" id="theNewName" />
</div>
</form>
</div>
$(".newName").click(function(){
var frm = $(this).closest("form");
$(".newContainer", frm).toggle(); // toggles the newContainer to show or hide
if($(this).text() == 'Remove'){$("#nameList", frm).val('');}
$(this).text($(this).text() == 'Add' ? 'Remove' : 'Add'); // changes our text
$("#nameList", frm).prop('disabled', $(this).text() != 'Add') // disables the original list if we are adding a new one
});
如果你担心文本("添加"/"删除")被更改+一些优化:
<a href="#" class="newName add" data-add-text="Add" data-remove-text="Remove">
Add
</a>
$('body').on('click',".newName",function(){
var frm = $(this).closest("form");
$(".newContainer", frm).toggle();
$("#nameList", frm).val('').prop('disabled', $(this).hasClass('remove'));
var newText = $(this).hasClass('remove')?
$(this).data('addText'):$(this).data('removeText');
$(this).text(newText).toggleClass('add remove');
});
$(".newName").click(function(){
var frm = $(this).closest("form");
$(".newContainer", frm).toggle(); // toggles the newContainer to show or hide
$(this).text($(this).text() == 'Add' ? 'Remove' : 'Add'); // changes our text
if($(this).text() == 'Remove' )
{
$("#theNewName",frm).val('');
}
$("#nameList", frm).prop('disabled', $(this).text() != 'Add') // disables the original list if we are adding a new one
});
请在此处查看演示:http://jsfiddle.net/
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何使用WCF服务和javascript表单post上传.doc文件
- Javascript生成的表单未提交
- 如何使用javascript或html下载PDF格式的填写表单
- HTML表单提交时未执行外部函数
- 如何将输入(type=text)从html表单传递到javascript函数
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- Ajax发布表单序列化,发布引号'
- 多级HTML表单
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 在验证和发送邮件后更改联系人表单的 html
- jQuery表单添加不适用于下拉列表
- Rails/JSON:如何将JSON用于jquery UI自动完成表单
- 如何从pdftron webviewer获取表单数据
- JS验证ajax返回的html中的表单数据
- Javascript更新孙窗口中的表单元素
- 解析javascript表单验证器
- 阻止表单元素提交
- jQuery,在toggle()中重置表单值
- 表单标签内的data-ui-toggle