简体   繁体   English

HTML不会在Bootstrap Modal中为表单字段动态添加值属性

[英]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. 我正在使用on-blur函数来执行此操作。 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(不显示value属性):

在此处输入图片说明

HTML for forms in dedicated page (value attribute correctly shown): 专用页面中表单的HTML(正确显示了value属性):

在此处输入图片说明

And also, jQuery doesn't detect the form fields in modal but detects the fields in the dedicated page. 而且,jQuery不会检测模式窗口中的表单字段,而是检测专用页面中的字段。

jQuery code : jQuery代码:

$(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 你应该解决这个问题

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

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