Bootstrap jquery如何清除模态输入字段以供重用

ars1skjm  于 2022-12-07  发布在  Bootstrap
关注(0)|答案(2)|浏览(134)

I have a modal form using jquery and twitter bootstraps. I have set up the modal so when the person cancels the form, / closes the modal i clear out the input fields. however if the person activates the modal again and enters values in to the fields, the dom still sees the field value as null.
here is my form html

<div class="modal fade in" id="forgotPassModal" data-b-view="ForgotPass" data-brite-cid="bview_1" tabindex="-1" aria-hidden="false" style="margin-top: -152.66666662693024px;">
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3>Security questions:</h3>
</div>
<div class="modal-body">
    <p>
        Please answer the following security questions, click 'Submit', and you
        will receive an email containing instructions on how to change your password.
    </p>
    <div class="form-horizontal">
        <fieldset id="forgotPassForm">

            <div class="control-group">
                <label class="span3" "label_4"="">What was the last name of your third grade teacher?</label>
                <div class="controls">
                    <input type="text" class="input-large" id="forgotPass_4">
                </div>
            </div>
            <div class="clearfix">&nbsp;</div>

            <div class="control-group">
                <label class="span3" "label_165"="">What is your maternal grandmother's maiden name, please provide full name?</label>
                <div class="controls">
                    <input type="text" class="input-large" id="forgotPass_165">
                </div>
            </div>
            <div class="clearfix">&nbsp;</div>

        </fieldset>
    </div>
</div>
<div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
    <button class="btn btn-primary forgotPassSubmit">Submit</button>
</div>

and here is my jquery

for (var i = 0; i < $forgotPassQuest.questionId.length; i++){
    var questionId = $forgotPassQuest.questionId[i]['id'];
    $('#forgotPass_' + questionId).val(null);
}

$('#forgotPassModal').modal({show:true})
$('.forgotPassSubmit').click(function() {
    var questAnswered = true;
    for (var i = 0; i < $forgotPassQuest.questionId.length; i++){
        var questionId = $forgotPassQuest.questionId[i]['id'];
        questionField = $('#forgotPass_' + questionId);
        questionFieldVal = questionField.val();
        if (questionFieldVal == '' || questionFieldVal == null){
            questAnswered = false;
        } else {}
    }
    if (questAnswered == true) {
        submitAnswers();
    } else {
        alert('please complete your security questions.');
    }
})
$('#forgotPassModal').on('hidden', function () {
    $('#forgotPassModal').remove();
});

now the form is dynamic and can change from user to user. Any ideas on a way t clear the form out on cancel/close but still let the value be recognized when the user does change the value?

5hcedyr0

5hcedyr01#

通过使用“remove”.remove(),可以有效地将整个DOM导入到垃圾桶中,从而删除所有事件侦听器。
因此,您应该只使用$('.modal-body', '#forgotPassModal').empty();隐藏模态或清除主体。

4zcjmb1e

4zcjmb1e2#

除了从DOM中删除div,您还可以“重置”输入值。
示例:http://jsfiddle.net/dMbXV/

$('#forgotPassModal').on('hidden', function () {
    $('input').val('');
});

相关问题