Using .change(), .focusout() and .when().then()
Update only after change, and lost focus.
HTML
<fieldset>
<legend>Some Form</legend>
<div id="last-action"><em>What's going on:</em><br /><br /></div>
<div class="editor-label">
<label for="Deadline">Deadline</label>
</div>
<div class="editor-field">
<input id="Deadline" name="Deadline" type="text" value="" />
</div>
<div class="editor-label">
<label for="Name">Task Name</label>
</div>
<div class="editor-field">
<input id="Name" name="Name" type="text" value="" />
</div>
<div class="editor-label">
<label for="Description">Description</label>
</div>
<div class="editor-field">
<input id="Description" name="Description" type="text" value="" />
</div>
</fieldset>
<select id='ts'>
<option value='a' df='1'>a</option>
<option value='b' df='2'>b</option>
<option value='c' df='3'>c</option>
</select>
<input type='checkbox' name='tc' value='1'> 1
<input type='checkbox' name='tc' value='2'> 2
<input type='checkbox' name='tc' value='3'> 3
<button id='click'>click</button>
</button>
CSS
div#last-action
{
float:right;
margin-right:50px;
height:200px;
width:200px;
background-color:black; color:white
}
fieldset
{
margin: 1em 0;
padding: 1em;
border: 1px solid #CCC;
}
fieldset p
{
margin: 2px 12px 10px 10px;
}
.display-label,
.editor-label,
.display-field,
.editor-field
{
margin: 0.5em 0;
}
.text-box
{
width: 30em;
}
JavaScript
//(function($) {
console.log(99999999999999.99);
$('input[name=tc]').change(function(){
if(!$(this).attr('checked')) return;
console.log($(this).val());
if($(this).val() == '3') {
$('input[name=tc]').removeAttr('checked');
$(this).click();
} else {
$('input[name=tc][value=3]').removeAttr('checked');
}
});
$('#click').click(function(){
var selected = [];
$('input[name=tc]:checked')
$('input[name=tc]').removeAttr('checked').attr('disabled', true);
});
function roundDownAndFormatStr(n, dp){
n = (n + "").replace(/^0+/, '');
if(n == '' || n.charAt(0) == '.') {
n = '0' + n;
}
console.log(n);
dp = parseInt(dp);
if(n.split('.').length > 2) n = '0'; else n = n.replace(/[^0-9\.]/g, "");
if(n.includes('.')){
var currDP = n.length - n.indexOf('.') - 1;
if(dp > currDP){
n = n.padEnd(((dp - currDP) + n.length), '0');
} else if(dp < currDP){
n = n.substring(0, n.indexOf('.') + dp + 1)
}
} else if(dp > 0) {
n = (n + '.').padEnd(n.length + 1 + dp, '0');
}
console.log(n);
return dp != 0 ? n.replace(/\B(?=(\d{3})+(?=\.))/g, ",") : n.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}