我想清除startTime
和endTime
值的值。问题是,textboxes
都有相同的名称,所以如果我清除它们,它们都会被清除。我不确定如何选择特定的实例,因为它们都具有相同的名称......这就是jquery.businesshours
的运作方式。
下面清除文本字段,但它会清除所有文本字段。
container.find("[name='startTime[]']").val('');
以下是javascript代码
/**
jquery.businessHours v1.0.1
https://github.com/gEndelf/jquery.businessHours
requirements:
- jQuery 1.7+
recommended time-picker:
- jquery-timepicker 1.2.7+ // https://github.com/jonthornton/jquery-timepicker
**/
(function($) {
$.fn.businessHours = function(opts) {
var defaults = {
preInit: function() {
},
postInit: function() {
},
inputDisabled: false,
checkedColorClass: "WorkingDayState",
uncheckedColorClass: "RestDayState",
colorBoxValContainerClass: "colorBoxContainer",
weekdays: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
operationTime: [
{},
{},
{},
{},
{},
{isActive: false},
{isActive: false}
],
defaultOperationTimeFrom: '',
defaultOperationTimeTill: '',
defaultActive: !0,
//labelOn: "Working day",
//labelOff: "Day off",
//labelTimeFrom: "from:",
//labelTimeTill: "till:",
containerTmpl: '<div class="clean"/>',
dayTmpl: '<div class="dayContainer">' +
'<div data-original-title="" class="colorBox"><input type="checkbox" class="invisible operationState"/></div>' +
'<div class="weekday"></div>' +
'<div class="operationDayTimeContainer">' +
'<div class="operationTime"><input type="text" name="startTime[]" class="mini-time operationTimeFrom" value=""/></div>' +
'<div class="operationTime"><input type="text" name="endTime[]" class="mini-time operationTimeTill" value=""/></div>' +
'</div></div>'
};
var container = $(this);
function initTimeBox(timeBoxSelector, time, isInputDisabled) {
timeBoxSelector.val(time);
if(isInputDisabled) {
timeBoxSelector.prop('readonly', true);
}
}
var methods = {
getValueOrDefault: function(val, defaultVal) {
return (jQuery.type(val) === "undefined" || val == null) ? defaultVal : val;
},
init: function(opts) {
this.options = $.extend(defaults, opts);
container.html("");
if(typeof this.options.preInit === "function") {
this.options.preInit();
}
this.initView(this.options);
if(typeof this.options.postInit === "function") {
//$('.operationTimeFrom, .operationTimeTill').timepicker(options.timepickerOptions);
this.options.postInit();
}
return {
serialize: function() {
var data = [];
container.find(".operationState").each(function(num, item) {
var isWorkingDay = $(item).prop("checked");
var dayContainer = $(item).parents(".dayContainer");
data.push({
isActive: isWorkingDay,
timeFrom: isWorkingDay ? dayContainer.find("[name='startTime[]']").val() : null,
timeTill: isWorkingDay ? dayContainer.find("[name='endTime[]']").val() : null
});
});
return data;
}
};
},
initView: function(options) {
var stateClasses = [options.checkedColorClass, options.uncheckedColorClass];
var subContainer = container.append($(options.containerTmpl));
var $this = this;
for(var i = 0; i < options.weekdays.length; i++) {
subContainer.append(options.dayTmpl);
}
$.each(options.weekdays, function(pos, weekday) {
// populate form
var day = options.operationTime[pos];
var operationDayNode = container.find(".dayContainer").eq(pos);
operationDayNode.find('.weekday').html(weekday);
var isWorkingDay = $this.getValueOrDefault(day.isActive, options.defaultActive);
operationDayNode.find('.operationState').prop('checked', isWorkingDay);
var timeFrom = $this.getValueOrDefault(day.timeFrom, options.defaultOperationTimeFrom);
initTimeBox(operationDayNode.find('[name="startTime[]"]'), timeFrom, options.inputDisabled);
var endTime = $this.getValueOrDefault(day.timeTill, options.defaultOperationTimeTill);
initTimeBox(operationDayNode.find('[name="endTime[]"]'), endTime, options.inputDisabled);
});
container.find(".operationState").change(function() {
var checkbox = $(this);
var boxClass = options.checkedColorClass;
var timeControlDisabled = false;
if(!checkbox.prop("checked")) {
// disabled
boxClass = options.uncheckedColorClass;
timeControlDisabled = true;
}
checkbox.parents(".colorBox").removeClass(stateClasses.join(' ')).addClass(boxClass);
checkbox.parents(".dayContainer").find(".operationTime").toggle(!timeControlDisabled);
}).trigger("change");
if(!options.inputDisabled) {
container.find(".colorBox").on("click", function() {
var checkbox = $(this).find(".operationState");
checkbox.prop("checked", !checkbox.prop('checked')).trigger("change");
});
}
}
};
return methods.init(opts);
};
})(jQuery);
答案 0 :(得分:0)
根据用于清除输入的注释中的以下代码:
if(!options.inputDisabled) {
container.find(".colorBox").on("click", function() {
var checkbox = $(this).find(".operationState");
checkbox.prop("checked", !checkbox.prop('checked')).trigger("change");
container.find(.dayContainer[data-weekday="4"] [name='startTime[]']).val('');
container.find(.dayContainer[data-weekday="4"] [name='endTime[]']).val('');
});
我们可以更改onClick处理程序,以更改与.colorBox
具有相同父级的开始和结束时间。
if (!options.inputDisabled) {
container.find(".colorBox").on("click", function(event) {
var checkbox = $(this).find(".operationState");
checkbox.prop("checked", !checkbox.prop('checked')).trigger("change");
var $dayContainer = $(event.target).closest('.dayContainer');
$dayContainer.find('[name="startTime[]"]').val('');
$dayContainer.find('[name="endTime[]"]').val('');
});
}