如何动态添加和删除要由Parsley.js验证的表单字段?

Mic*_*nch 14 validation jquery dynamic parsley.js

我有一个表格('#registrations'),我正在与Parsley.js验证,到目前为止它工作正常.但是,我正在尝试动态删除表单字段并将新的字段添加到Parsley验证中,具体取决于某人在选择下拉列表中选择的内容('#manureurer').

这是我的标记:

<select name="manufacturer" id="manufacturer" parsley-required="true" data-error-message="Please select a manufacturer.">

    <option value="apple">Apple</option>

    <option value="blackberry">Blackberry</option>

    <option value="htc">HTC</option>

    <option value="huawei">Huawei</option>

    <option value="lg">LG</option>

    <option value="motorola">Motorola</option>

    <option value="nokia">Nokia</option>

    <option value="samsung">Samsung</option>

    <option value="sony">Sony</option>

    <option value="sony-ericsson">Sony Ericsson</option>

</select>
Run Code Online (Sandbox Code Playgroud)

这是我的JS:

//init parsley
$('#registrations').parsley();

$('#manufacturer').change(function() {

        //define selected value
        var manufacturer = $(this).val();

        //destroy parsley
        $('#registrations').parsley('destroy');

        //remove all models selects from parsley validation
        //if someone has previously selected a different manufacturer
        $('#registrations').parsley('removeItem', '#apple-models');
        $('#registrations').parsley('removeItem', '#blackberry-models');
        $('#registrations').parsley('removeItem', '#htc-models');
        $('#registrations').parsley('removeItem', '#huawei-models');
        $('#registrations').parsley('removeItem', '#lg-models');
        $('#registrations').parsley('removeItem', '#motorola-models');
        $('#registrations').parsley('removeItem', '#nokia-models');
        $('#registrations').parsley('removeItem', '#samsung-models');
        $('#registrations').parsley('removeItem', '#sony-models');
        $('#registrations').parsley('removeItem', '#sony-ericsson-models');

        //add corresponding models select to parsely
        $('#registrations').parsley('addItem', '#'+manufacturer+'-models');

        //reinit parsley
        $('#registrations').parsley();

    });
Run Code Online (Sandbox Code Playgroud)

这不起作用,但我不知道为什么.

Mic*_*nch 40

将新字段添加到Parsley后,您需要向该字段添加所需的约束.

//add corresponding models select to parsely
$('#registrations').parsley('addItem', '#'+manufacturer+'-models');

//add required constraint 
$('#'+manufacturer+'-models').parsley('addConstraint', {
    required: true 
});
Run Code Online (Sandbox Code Playgroud)

更新(2014年4月10日)

以上适用于Parsley.js 1.x,但不适用于Parsley 2.x.

香菜2 x不使用addItem,removeItem,addConstraint,或removeConstraint.

相反,Parsley 2.x将根据每个输入的数据属性自动检测表单中的更改.在上面的示例中,如果要向Parsley添加新项,则可以执行以下操作:

//destroy parsley
$('form').parsley().destroy();

//set required attribute on input to true
$('input').attr('data-parsley-required', 'true');

//reinitialize parsley
$('form').parsley();
Run Code Online (Sandbox Code Playgroud)

同样,如果你想从Parsley中删除一个项目,你会这样做:

//destroy parsley
$('form').parsley().destroy();

//set required attribute on input to false
$('input').attr('data-parsley-required', 'false');

//reinitialize parsley
$('form').parsley();
Run Code Online (Sandbox Code Playgroud)

  • 难道你不能只做一个`reset()`而不是destroy和reinit?> reset()#2.0重置此表单及其字段的UI. (2认同)
  • 不工作 ``$('.form').parsley().isValid()``是``false``,当表项目被删除时提交.有什么建议? (2认同)