Styling the field to contain symbols
When it comes to including the symbols within this number
field, you can use some walkaround like that:
HTML:
<span id="number-container">
<input type="number" name="number" id="number-field" value="500" />
<span id="number-container-symbol">$</span>
</span>
CSS:
#number-container {
position: relative;
}
#number-container-symbol {
left: 5pt;
position: absolute;
top: 0px;
}
#number-field {
background-color: transparent;
padding-left: 10pt;
}
Treat it as a proof of concept. See this jsfiddle for a live example. It looks like that in Chrome:
Defining smaller granularity
Based on the documentation on number input (Editor's Draft), to define granularity you need to add step="<some-floating-point-number>"
attribute to the <input>
tag:
<input type="number" name="number" value="500.01" step="0.01" />
and it will work in many modern browsers. See this jsfiddle for tests.
Conclusion
You should be able to style it to contain symbols you need. There is also a feature that, according to documentation, enables support for floating-point numbers.
Alternative solution - empty field in front of something else
You can also trick someone into believing he is seeing content of the input field, but show him something else behind the field (this is some extension of my original solution). Then if someone taps the field, you can propagate proper value etc., then go back to the previous state on blur. See this code (live example on jsfiddle - blue color means you are looking at something that is not within the field):
// store original value from INPUT tag in jQuery's .data()
var input_field = jQuery('#number-field');
var display_span = jQuery('#number-container-value');
var base_val = parseFloat(input_field.val());
input_field.data('storedVal', base_val);
input_field.val('');
display_span.html(base_val);
// react to field gaining and losing focus
jQuery('#number-field').on('focus', function(event){
var el = jQuery(this);
var display = jQuery('#number-container-value');
if (typeof el.data('storedVal') != 'undefined'){
el.val(el.data('storedVal'));
};
display.html('');
}).on('blur', function(event){
var el = jQuery(this);
var display = jQuery('#number-container-value');
var new_val = parseFloat(el.val());
el.data('storedVal', new_val);
display.html(new_val);
el.val('');
});
(the full code with styling is on the mentioned jsfiddle). The code needs shortening & cleaning up, but it is a proof of concept. Try it and share your findings, please.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…