Alternativní typy vstupu HTML5

Jak možná víte, HTML5 zavedlo několik nových typů vstupu:číslo, datum, barva, rozsah atd. Otázka zní:měli byste tyto ovládací prvky začít používat nebo ne? I když chci říct „Ano“, myslím, že ještě nejsou připraveni na žádný skutečný projekt. Hlavním důvodem je nekonzistentní implementace různými prohlížeči.

Zobrazit ukázku

Ve formuláři níže můžete vidět některé typy vstupu HTML5. V závislosti na vašem prohlížeči můžete nebo nemusíte vidět žádný rozdíl od běžného ovládacího prvku vstupu.

Co byste tedy měli použít? Můžete vyvinout své vlastní typy vstupů nebo použít existující knihovnu. Každý pravděpodobně zná výběr data jQuery nebo jiné zásuvné moduly jQuery, které přicházejí na záchranu. Zatím jsem však nenašel komplexní knihovnu, která by vyhovovala všem mým vstupním potřebám, a tak jsem se rozhodl sestavit svou vlastní, která by byla malá, konzistentní a pokrývala následující oblasti:

  • Číselné vstupy
  • Datum a čas
  • Rozbalovací seznamy
  • Vícenásobný výběr
  • Nahrání souboru

Na těchto ovládacích prvcích jsem pracoval v průběhu několika posledních let jako součást velké knihovny nazvané W2UI. Uvědomil jsem si však, že samostatná knihovna pouze s ovládacími prvky vstupu může být docela užitečná.

Číselné vstupy

Číselné vstupy vám umožní zadávat pouze čísla. Všechny ostatní postavy budou zcela ignorovat. Je implementována plná podpora klávesnice. Zkuste použít up /dolů šipky, ovládací prvek + nahoru /dolů (příkaz + nahoru /dolů na Macu) pro zvýšení počtu. Když se číslo změní, bude ověřeno a naformátováno (v případě potřeby).

Nastavení HTML

<!-- General inputs -->
<div class="w2ui-label"> Integer: </div>
<div class="w2ui-field"> <input id="w2int"> </div>
<div class="w2ui-label"> Float: </div>
<div class="w2ui-field"> <input id="w2float"></div>
<div class="w2ui-label"> Hex: </div>
<div class="w2ui-field"> <input id="w2hex"></div>
<div class="w2ui-label"> Color: </div>
<div class="w2ui-field"> <input id="w2color"></div>

<!-- US format -->
<div class="w2ui-label"> Integer: </div>
<div class="w2ui-field"> <input id="us-int" value="0"> </div>
<div class="w2ui-label"> Float: </div>
<div class="w2ui-field"> <input id="us-float" value="0"> </div>
<div class="w2ui-label"> Money: </div>
<div class="w2ui-field"> <input id="us-money" value="0"> </div>
<div class="w2ui-label"> Percent: </div>
<div class="w2ui-field"> <input id="us-percent" value="0"> </div>

<!-- EU common format -->
<div class="w2ui-label"> Integer: </div>
<div class="w2ui-field"> <input id="eu-int" value="0"> </div>
<div class="w2ui-label"> Float: </div>
<div class="w2ui-field"> <input id="eu-float" value="0"> </div>
<div class="w2ui-label"> Money: </div>
<div class="w2ui-field"> <input id="eu-money" value="0"> </div>
<div class="w2ui-label"> Percent: </div>
<div class="w2ui-field"> <input id="eu-percent" value="0"> </div>

Vytvoření interaktivních polí

// General
$('#w2int').w2field('int', { autoFormat: false });
$('#w2float').w2field('float', { autoFormat: false });
$('#w2hex').w2field('hex');
$('#w2color').w2field('color');

// US Format
$('#us-int').w2field('int', { autoFormat: true });
$('#us-float').w2field('float', { precision: 3 });
$('#us-money').w2field('money', { moneySymbol: '$' });
$('#us-percent').w2field('percent', { precision: 1, min: 0, max: 100 });

// EU Common Format
$('#eu-int').w2field('int', { autoFormat: true, groupSymbol: ' ' });
$('#eu-float').w2field('float', { groupSymbol: ' ', precision: 3 });
$('#eu-money').w2field('money', { groupSymbol: ' ', currencyPrefix: '', currencySuffix: '€' });
$('#eu-percent').w2field('percent', { precision: 1, min: 0, max: 100 });

Druhý argument je seznam možností, které zahrnují následující:

options = {
	min             : null,
	max             : null,
	placeholder     : '',
	autoFormat      : true,
	currencyPrefix  : '$',
	currencySuffix  : '',
	groupSymbol     : ',',
	arrows          : false,
	keyboard        : true,
	precision       : null,
	silent          : true,
	prefix          : '',
	suffix          : ''
}

Datum a čas

U typů DATE a TIME můžete pomocí klávesnice zvýšit o den (nebo minutu), pokud kliknete na nahoru /dolů šipky. Můžete také použítctr + nahoru /dolů (příkaz + nahoru /dolů na mac) zvýšit o měsíc (nebo hodinu).

Nastavení HTML

<!-- US format -->
<div class="w2ui-label"> Date: </div>
<div class="w2ui-field"> <input type="us-date"> </div>
<div class="w2ui-label"> From-To: </div>
<div class="w2ui-field"> <input type="us-dateA"> <span class="legend">(from 10th to 20th of current month)</span></div>
<div class="w2ui-label"> Blocked Days: </div>
<div class="w2ui-field"> <input type="us-dateB"> <span class="legend">(12,13,14 of current month are blocked)</span></div>
<div class="w2ui-label"> Date Range: </div>
<div class="w2ui-field"> <input type="us-date1"> - <input type="us-date2"> </div>
<div class="w2ui-label"> Time: </div>
<div class="w2ui-field"> <input type="us-time"> </div>
<div class="w2ui-label"> From-To: </div>
<div class="w2ui-field"> <input type="us-timeA"> <span class="legend">(from 8:00 am to 4:30 pm)</span></div>

<!-- EU common format -->
<div class="w2ui-label"> Date: </div>
<div class="w2ui-field"> <input type="eu-date"> </div>
<div class="w2ui-label"> From-To: </div>
<div class="w2ui-field"> <input type="eu-dateA"> <span class="legend">(from 10th to 20th of current month)</span></div>
<div class="w2ui-label"> Blocked Days: </div>
<div class="w2ui-field"> <input type="eu-dateB"> <span class="legend">(12,13,14 of current month are blocked)</span></div>
<div class="w2ui-label"> Date Range: </div>
<div class="w2ui-field"> <input type="eu-date1"> - <input type="eu-date2"> </div>
<div class="w2ui-label"> Time: </div>
<div class="w2ui-field"> <input type="eu-time"> </div>
<div class="w2ui-label"> From-To: </div>
<div class="w2ui-field"> <input type="eu-timeA"> <span class="legend">(from 8:00 am to 4:30 pm)</span></div>
<div style="height: 20px; clear: both"></div>

Vytvoření interaktivních polí

var month = (new Date()).getMonth() + 1;
var year  = (new Date()).getFullYear();

// US Format
$('input[type=us-date]').w2field('date');
$('input[type=us-dateA]').w2field('date', { format: 'm/d/yyyy', start:  month + '/5/' + year, end: month + '/25/' + year });
$('input[type=us-dateB]').w2field('date', { format: 'm/d/yyyy', blocked: [ month+'/12/2014',month+'/13/2014',month+'/14/' + year,]});
$('input[type=us-date1]').w2field('date', { format: 'm/d/yyyy', end: $('input[type=us-date2]') });
$('input[type=us-date2]').w2field('date', { format: 'm/d/yyyy', start: $('input[type=us-date1]') });
$('input[type=us-time]').w2field('time',  { format: 'h12' });
$('input[type=us-timeA]').w2field('time', { format: 'h12', start: '8:00 am', end: '4:30 pm' });

// EU Common Format
$('input[type=eu-date]').w2field('date',  { format: 'd.m.yyyy' });
$('input[type=eu-dateA]').w2field('date', { format: 'd.m.yyyy', start:  '5.' + month + '.' + year, end: '25.' + month + '.' + year });
$('input[type=eu-dateB]').w2field('date', { format: 'd.m.yyyy', blocked: ['12.' + month + '.' + year, '13.' + month + '.' + year, '14.' + month + '.' + year]});
$('input[type=eu-date1]').w2field('date', { format: 'd.m.yyyy', end: $('input[type=eu-date2]') });
$('input[type=eu-date2]').w2field('date', { format: 'd.m.yyyy', start: $('input[type=eu-date1]') });
$('input[type=eu-time]').w2field('time',  { format: 'h24' });
$('input[type=eu-timeA]').w2field('time', { format: 'h24', start: '8:00 am', end: '4:30 pm' });

Možnosti pro datum

options = {
	format      : 'm/d/yyyy',  // date format
	placeholder : '',
	keyboard    : true,
	silent      : true,
	start       : '',          // string or jquery object
	end         : '',          // string or jquery object
	blocked     : {},          // { '4/11/2011': 'yes' }
	colored     : {}           // { '4/11/2011': 'red:white' }
};

Možnosti pro čas

options = {
	format      : 'hh:mi pm',
	placeholder : '',
	keyboard    : true,
	silent      : true,
	start       : '',
	end         : ''
};

Rozbalovací seznamy

Běžný vstup