我试图从输入到表的单元格中获取值,我不知道为什么val(); 无法正常工作(无法获取价值)。我到处,在这里,在这里和这里都检查过,但收效甚微。我相信我的代码是正确的。我是否缺少渲染方面的内容,或者我的代码错误?所有帮助将不胜感激。
先感谢您
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap");
var add_button = $(".add_studies_button");
var studies = $("#studies").val();
var x = 1; //initlal text box count
$(add_button).click(function(e){
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++;
$(wrapper).append('<tr>' +
'<td>' + studies + '</td>' +
'<td>Que tal</td>' +
'<td>Pitt</td>' +
'<td>35</td>' +
'<td>New York</td>' +
'<td><a class="remove_field">Remove</a></td>' +
'</tr>');
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent().parent().remove(); x--;
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-horizontal pull-right col-xs-6">
<div class="form-group">
<label class="col-lg-3 control-label">Studies:</label>
<div class="col-lg-8">
<input class="form-control" maxlength="100" type="text" id="studies" required="required" placeholder="Studies">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<button class="btn btn-primary pull-right add_studies_button" style="margin-top:7px;">Add</button>
<span></span>
</div>
</div>
<div class="table-responsive col-xs-6">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Studies</th>
<th>School</th>
<th>From</th>
<th>To</th>
<th>Action</th>
</tr>
</thead>
<tbody class="input_fields_wrap">
</tbody>
</table>
</div>
尝试添加以下语句:
var studies = $("#studies").val();
click
侦听器内部:
$(add_button).click(function(e) {
这样可以在单击时获得#studies
输入框的值。代码中的变量是在开头设置的,永远不会更新(它不会自动更新为的值)。studies
#studies
在这里查看工作示例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句