简体   繁体   中英

How to validate dynamic generated input box value using javascript

I'm creating multiple textbox dynamically, I want to do textbox validation like (textbox should not be empty) while form submission.

I'm not able to capture textbox id or name on form submit jquery function, can someone help how can I validate all dynamically created textboxes using JavaScript/jquery.

I'm using below code to generate multiple textboxes dynamically:

var input = $("<input/>", {
    type: "text",
    name: "productPrice",
    class: "form-control",
    id: obj.productName + "Id",
    value: obj.productPrice
});

Thanks for your help in advance.

Adapted from Ranjeet Singh's fiddle: https://jsfiddle.net/kjdx9gfw/2/

$('#form_submit').find('input').each(function(i, input){
    var $input = $(input);
    $input.css('background-color', $input.val() ? 'green' : 'red');
});

Basically, it is iterating through every input inside the form .

我不确定你究竟想做什么,但也许如果你把<input required/>你得到的文本框不应该是空的

Here is the jsfiddle link Which is working according your . 

https://jsfiddle.net/sanjayarakeri/fqkh398h/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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