简体   繁体   English

如何在模糊时更改输入值?

[英]how to change the input value on blur?

I am having one link as 'Add more' which adds input element as many as I want. 我有一个链接为“添加更多”,可根据需要添加任意多个输入元素。 I want to call blur function on that. 我想对此调用模糊功能。

Following html gets added while click on 'Add more' link: 在单击“添加更多”链接时添加以下html:

<input required="" class="form-control js-validate-hoursToAdd timetoadd" step="0.01" name="calculations[settingIndex][hoursToAdd][calculationIndex]" type="number" value="">

Blur event works only for first element which is there in DOM by default. 默认情况下,模糊事件仅适用于DOM中存在的第一个元素。 When I add new element, blur event doesn't get bind to the element. 当我添加新元素时,blur事件不会绑定到该元素。

Following is the javascript code. 以下是javascript代码。

$(document).ready(function() {
    $(".timetoadd").blur(function(){
    this.value = parseFloat(this.value).toFixed(2);
    });
)};

It is in separate file called as backend.js. 它在称为backend.js的单独文件中。 I am using webpack to minify the file and it is included in html file. 我正在使用webpack缩小文件,它包含在html文件中。

How to do that? 怎么做? Please help me out. 请帮帮我。

Here's an example I've made for you. 这是我为您制作的一个示例。 Do this for your input. 为此,请您输入。 It should work fine 应该很好

 function GetValue(e){ alert(e.target.value); }; 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="inputgroup"> <input type="text" name="Dynamic_0" onblur="GetValue(event)"> <input type="text" name="Dynamic_1" onblur="GetValue(event)"> <input type="text" name="Dynamic_2" onblur="GetValue(event)"> </div> 

Use jQuery's on() method on a parent element with an additional selector as the second argument: on()具有附加选择器作为第二个参数的元素on()使用jQuery的on()方法:

 $(document).ready(function() { $("#btnAdd").click(function() { $('<br/><input required="" class="form-control js-validate-hoursToAdd timetoadd" step="0.01" name="calculations[settingIndex][hoursToAdd][calculationIndex]" type="number" value="">').appendTo(document.body); }); $(document.body).on('blur', '.timetoadd', function(){ this.value = parseFloat(this.value).toFixed(2); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="button" id="btnAdd">Add more</button> <br/><input required="" class="form-control js-validate-hoursToAdd timetoadd" step="0.01" name="calculations[settingIndex][hoursToAdd][calculationIndex]" type="number" value=""> 

Instead of document.body , you could also use any other parent that contains the inputs. 除了document.body ,您还可以使用包含输入的任何其他父项。

By adding the html attribute onblur and some javascript... 通过添加html属性onblur和一些javascript ...

 function myFunc(input) { input.value = 0; } 
 <input required="" class="form-control js-validate-hoursToAdd timetoadd" step="0.01" name="calculations[settingIndex][hoursToAdd][calculationIndex]" type="number" value="" onblur="myFunc(this)"> 

 // find elements var id = $("#id > div") var id1 = $("#id1") var input = $("input") var inputCopy; var button = $("button") // handle click and add class button.on("click", function() { $('<input required="" class="form-control js-validate-hoursToAdd timetoadd" step="0.01" name="calculations[settingIndex][hoursToAdd][calculationIndex]" type="number" value=""><br>').appendTo(id); /* addMore(e.currentTarget, e.currentTarget.value); console.log('new input', e.currentTarget); */ }) $(document.body).on("blur", '.timetoadd', function() { this.value = parseFloat(this.value).toFixed(2); inputCopy = input; }) function addMore(e, value) { inputCopy = e.clone() id.prepend($(inputCopy)); } 
 body { background: #20262E; padding: 20px; font-family: Helvetica; } #id { background: #fff; border-radius: 4px; padding: 20px; font-size: 25px; text-align: center; transition: all 0.2s; margin: 0 auto; width: 300px; } button { background: #0084ff; border: none; border-radius: 5px; padding: 8px 14px; font-size: 15px; color: #fff; } #id.alt { background: #0084ff; color: #fff; margin-top: 40px; width: 200px; } #id.alt button { background: #fff; color: #000; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="id"> <button>Add more</button> <div> <input required class="form-control js-validate-hoursToAdd timetoadd" step="0.01" name="calculations[settingIndex][hoursToAdd][calculationIndex]" type="number" value=""> </div> </div> 

JSFiddle : https://jsfiddle.net/kutec/c4pvLoua/ JSFiddlehttps : //jsfiddle.net/kutec/c4pvLoua/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM