【问题标题】:How to dynamically pass an id to url of Bootstrap validator rule: remote for a same form input field?如何动态地将 id 传递给 Bootstrap 验证器规则的 url:远程用于相同的表单输入字段?
【发布时间】:2018-10-12 13:08:45
【问题描述】:

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',除非页面重新加载。

【问题讨论】:

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


    【解决方案1】:

    找到了解决办法!

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

    <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' 是为远程方法动态传递的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 2021-06-24
      • 2018-06-29
      • 1970-01-01
      • 2020-02-01
      相关资源
      最近更新 更多