Javascript 如何将文本字段从模式传递到父级?
当按下父窗口上的按钮时,模式窗口向下滑动。模式窗口有两个文本字段。用户完成文本输入后,有一个“添加”按钮可供按下。我希望add按钮以html格式填写无序列表。我怎样才能把它传给家长Javascript 如何将文本字段从模式传递到父级?,javascript,html,twitter-bootstrap,modal-dialog,parent-child,Javascript,Html,Twitter Bootstrap,Modal Dialog,Parent Child,当按下父窗口上的按钮时,模式窗口向下滑动。模式窗口有两个文本字段。用户完成文本输入后,有一个“添加”按钮可供按下。我希望add按钮以html格式填写无序列表。我怎样才能把它传给家长 <div class="modal fade" id="newTask" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog">
<div class="modal fade" id="newTask" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Add a New Task</h4>
</div>
<div class="modal-body">
<form>
<input class="textField" type="text" placeholder="Enter the Task here."></input>
<input class="textField datepicker dueDate" id="dpd1" type="text" placeholder="Select Date">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="addListItem('list')" id="addTask">Add Task</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
$('.modal form').on('#newTask', function addListItem(listID){
var input = $('input[type=text]');
var str = '<li><table id="todoTable" border="1" cellspacing="2" cellpadding="3"><tr><td> <input type="checkbox" class="checkbox"> </input> </td><td> <input type="text" class="todo-data"> </input> </td><td> <input type="button" class = "image" value="X"> </input> </td></tr><tr><td> <input type="checkbox" class="no-show"> </input> </td> <script>$function(){$(".datepicker").datepicker();});</script> <td> <input type="text" id="datepicker" class = "datepicker"> </input> </td></tr></table></li>';
$('.todo-list').append(str);
input.val('');
$(this).parent().hide();
return false;
&时代;
添加新任务
接近
添加任务
$('.modal form')。在('#newTask',函数addListItem(listID){
变量输入=$('input[type=text]');
var str='$function(){$(“.datepicker”).datepicker();}); ';
$('.todo list')。追加(str);
input.val(“”);
$(this.parent().hide();
返回false;
})) 正如第一条注释所提到的,您所需要做的就是将其附加到列表中。我不确定你对哪一部分感到困惑,所以我把一个简单的例子放在一起,以尽可能少的方式完成上述所有工作。JSFIDLE的链接如下: 下面是复制的javascript代码:
$('button').on('click', function(){
$('.modal').toggle()
});
$('.modal form').on('submit', function(){
var input = $('input[type=text]');
$('ul').append('<li>'+ input.val() + '</li>');
input.val('');
$(this).parent().hide();
return false;
});
$('button')。在('click',function()上{
$('.modal').toggle()
});
$('.modal form')。在('submit',function()上{
变量输入=$('input[type=text]');
$('ul').append(''+input.val()+' ');
input.val(“”);
$(this.parent().hide();
返回false;
});
第一次单击功能很简单,单击按钮时只需打开或关闭模式。第二部分是大部分动作发生的地方。提交模式中的表单时,下面是逐行进行的操作:
- 抓取用户输入文本的输入字段
- 将该字段的值添加到列表中,并用列表项包装
- 清除文本字段,以便下次需要添加项目时该字段为空
- 关闭模式
- 通过返回false取消实际表单提交
开放模态
...
//首先,我们需要确保当您
//点击按钮
$('.triggerModal')。在('click',function()上{
$('#newTask').modal()
});
//现在,在模式启动之后,我们需要对表单提交做出响应。
//我会让你根据我的例子来填写。如果这仍然令人困惑
//对于您来说,您需要回顾一下javascript和jquery的基础知识。如果这
//如果是这样的话,请告诉我,我很乐意提供资源建议
编辑2:作者表示,他们并不真正了解html、css和javascript的基本工作原理,因此,虽然这个答案可能会解决问题,但超出了范围。下面是一些我建议开始学习html、css和javascript基础知识的资源:
- HTML:
- CSS:
- JS基础知识:
- jQuery基础知识:
<!-- table up top here -->
<table id="todoTable" border="1" cellspacing="2" cellpadding="3">
<tr>
<td><input type="checkbox" class="checkbox" /></td>
<td><input type="text" class="todo-data" /></td>
<td><input type="button" class="image" value="X" /></td>
</tr>
<tr>
<td><input type="checkbox" class="no-show" /></td>
<td><input type="text" id="datepicker" class="datepicker" /></td>
</tr>
</table>
<!-- we need a button to click that makes the modal show up -->
<button class='triggerModal'>open modal</button>
<!-- modal html here (abbreviated) -->
<div id='newTask'> ... </div>
<!-- now we can start on the script -->
<script>
// first, we need to make sure the modal shows up when you
// click the button
$('.triggerModal').on('click', function(){
$('#newTask').modal()
});
// now, after the modal is up, we need to respond to the form submission.
// i'll let you fill this in, based on my example. If this is still confusing
// to you, you need to go back and review javascript and jquery basics. if this
// is the case let me know and I'd be happy to suggest resources
</script>