【问题标题】:bootstrap datepicker not working inside of a javascript引导日期选择器在 javascript 中不起作用
【发布时间】:2016-09-09 09:59:17
【问题描述】:

为什么引导程序不能在 javascript 内部工作,但在外部工作。

这是我的代码:

var date = new Date();
		date.setDate(date.getDate());

$('.datepicker').datepicker({
    	startDate: date,
        autoclose: true,
        format: "mm-dd-yyyy",
        todayHighlight: true,
        orientation: "top auto",
        todayBtn: true,
        todayHighlight: true,  
    });
 var rowCount = 1;

function removeRow(removeNum) {
jQuery('#rowCount'+removeNum).remove();
rowCount --;
rcount();
}

function addMoreRows(frm) {
rowCount ++;
var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input id="datepkr" class="form-control datepicker" style="width:60%; float:left;" name="" type="text"/></td><td><a style="float:left;" href="javascript:void(0);" onclick="removeRow('+rowCount+');"><i class="glyphicon glyphicon-remove" style="color:red;"></i></a></td></tr></p>';
jQuery('#addedRows').append(recRow);

		if (rowCount>15){
			$('#btnAdd').prop('disabled', true);
		}

}
function rcount(){
	if (rowCount==3){
		jQuery('#x2').hide('slow');
		jQuery('#x3').show('slow');	
		}
		if (rowCount==4){
		jQuery('#x3').hide('slow');
		jQuery('#x4').show('slow');	
		}
		if (rowCount==5){
		jQuery('#x4').hide('slow');
		jQuery('#x5').show('slow');	
		}
		if (rowCount==6){
		jQuery('#x5').hide('slow');
		jQuery('#x6').show('slow');	
		}
		if (rowCount==7){
		jQuery('#x6').hide('slow');
		jQuery('#x7').show('slow');	
		}
		if (rowCount==8){
		jQuery('#x7').hide('slow');
		jQuery('#x8').show('slow');	
		}
		if (rowCount==9){
		jQuery('#x8').hide('slow');
		jQuery('#x9').show('slow');	
		}
		if (rowCount==10){
		jQuery('#x9').hide('slow');
		jQuery('#x10').show('slow');	
		}
		if (rowCount==11){
		jQuery('#x10').hide('slow');
		jQuery('#x11').show('slow');	
		}
		if (rowCount==12){
		jQuery('#x11').hide('slow');
		jQuery('#x12').show('slow');	
		}
		if (rowCount==13){
		jQuery('#x12').hide('slow');
		jQuery('#x13').show('slow');	
		}
		if (rowCount==14){
		jQuery('#x13').hide('slow');
		jQuery('#x14').show('slow');	
		}
		if (rowCount==15){
		jQuery('#x14').hide('slow');
		jQuery('#x15').show('slow');	
		}
		if (rowCount==16){
		jQuery('#x15').hide('slow');
		jQuery('#x16').show('slow');	
		}
		
		if (rowCount>14){
			$('#btnAdd').prop('disabled', true);
		}
		else{
			$('#btnAdd').prop('disabled', false);
		}
}
<script   src="https://code.jquery.com/jquery-3.1.0.js"   integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk="   crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/blogController.js"></script>

<table>	
	<tr id="rowId">
	<td><input name="" type="text"  value="" class="datepicker"/></td>
	<td><button type="button" id="btnAdd" onclick="addMoreRows()" class="btn btn-primary"><i class="glyphicon glyphicon-plus"></i>ADD</button></td>
	</table>
	<div id="addedRows"></div>
</td>
</tr>

Here it is Showing the Datepicker but in a javascript generated input its not showing

希望有人能提供帮助。谢谢。

【问题讨论】:

  • 我不完全确定我理解你的意思它没有显示“内部 javascript”。 JavaScript 的内部在哪里,外部在哪里?
  • 动态添加新行后需要重新初始化DatePicker。我认为@Hardik Patel 的答案会奏效。祝你好运。
  • 首先,您的第一行初始化不正确。具体来说,这个:$('datepicker').. 什么是 datepicker?一个类(在它前面添加一个 .)或一个 ID(在它前面添加一个 #)?
  • 对不起,我刚刚更新了代码。运行它并单击添加。并且在输入上它没有显示日期选择器。现在我的问题是为什么即使我使用同一个类它也不起作用(日期选择器)。 (表单控制日期选择器)?。我在 javascript 中的意思是在 onclick 事件上创建的输入。

标签: javascript jquery twitter-bootstrap datepicker


【解决方案1】:

您需要更正您的 JS 代码。请尝试以下代码。

$(document).ready(function() {
    bindDatePicker($('.datepicker'));

  $('button#btnAdd').click(function() {
    var recRow = '<tr><td><input class="form-control datepicker" style="width:60%; float:left;" name="" type="text"/></td></tr>';
    alert(recRow);
        jQuery('#addedRows').append(recRow);
        bindDatePicker($('.datepicker'));
        if (rowCount>15){
            $('#btnAdd').prop('disabled', true);
        }
  });
});

function bindDatePicker(element) {
        $(element).datepicker({         
            autoclose: true,
            format: "mm-dd-yyyy",
            todayHighlight: true,
            orientation: "top auto",
            todayBtn: true,
            todayHighlight: true,  
        });
}

您可以在这里查看演示:JSFIDDLE

【讨论】:

    猜你喜欢
    • 2013-08-28
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    相关资源
    最近更新 更多