javascript - 如何将 id 动态传递到 Bootstrap 验证器规则 : remote for a same form input field? 的 url

标签 javascript twitter-bootstrap-3 bootstrapvalidator jqbootstrapvalidation bootstrapvalidator-1000hz

Bootstrap Validator v 0.5.2 被重新用于验证模式中的表单 (#myForm)。 当表单加载到如下模式时,需要动态地将唯一 ID(外键)传递给“远程”规则的“url”。

var remoteUrl = "/remoteurl/";
var id = <Foreign key of the record>

$('#myForm').bootstrapValidator({
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        fieldName: {
            validators: {
                remote: {
                    url: remoteUrl + id, //dynamically passing id. // but not passing dynamically.
                    type: 'POST',
                    message: "This is the message!"
                }
            }
        } 
    }
});

问题:
在模态加载时,“id”成功地动态传递到表单中。但是,“bootstrapValidator”获取表单中第一个传递的“id”,除非页面重新加载。

最佳答案

找到解决方案!

为添加外键添加一个隐藏的输入字段。

<input type="hidden" value="" name="foreignKey" id="foreignId">

并且,动态地将外键传递给这个字段。

$('#foreignId').val(id);

然后,如下

fieldName: {
    validators: {
        remote: {
            url: remoteUrl,
             data: function(validator, $field, value) {
                return {
                    foreignKey: validator.getFieldElements('foreignKey').val()
                };
            },
            type: 'POST',
            message: "This is the message!"
        }
    }
}

现在,它对我有用。 'Id' 为远程方法动态传递。

关于javascript - 如何将 id 动态传递到 Bootstrap 验证器规则 : remote for a same form input field? 的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50134376/

相关文章:

javascript - RxJS 如何返回带有默认错误处理函数的 Observable

javascript - React Bootstrap 不断显示工具提示

javascript - 单击按钮时隐藏/显示隐藏的 div

html - 如何在 Bootstrap 的 div 下制作三 Angular 形?

html - Bootswatch/Bootstrap CSS/主题在 IE7 和 IE8 中中断页面

javascript - 渲染页面时如何从 Handlebars 获取 Angular 数据

css - 在 Bootstrap 中将相对 div 居中到窗口

javascript - BootstrapValidator 未显示成功消息

html - 如何使用 Bootstrap 验证器在 Bootstrap v4 上进行验证?

bootstrapvalidator - Google INVISIBLE reCaptcha + Bootstrap 验证器