简体   繁体   中英

HTML is not dynamically adding value attributes for form fields in Bootstrap Modal

I have a Contact Me form which is displayed in the modal. When the inputs are incorrect, the user is directed to a dedicated Contact Me page.

Form in modal :

在此处输入图片说明

Dedicated page for the form :

在此处输入图片说明

I am trying to show input errors straightaway in the modal itself. I am using on-blur function to do this. The issue is that, the form field values aren't dynamically updated for forms in the modal. The form field values are however updated in the dedicated page.

HTML for forms in modal (value attribute not appearing) :

在此处输入图片说明

HTML for forms in dedicated page (value attribute correctly shown):

在此处输入图片说明

And also, jQuery doesn't detect the form fields in modal but detects the fields in the dedicated page.

jQuery code :

$(document).on('blur', '#id_name', function() {
    var name = $(this).val();
    alert(name);  // works only in the dedicated page !
});

Any help? Thanks !

When you execute your script, your modal isn't attached to the page yet so the elements can't be retrieved. Wrap all your script inside this:

$(document).ready(function() { //here all my code });

And you should fix the issue

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