문제

아래 jQuery 대화 상자 스크립트가 있습니다. 대화 상자를 닫을 때 양식을 지우는 함수를 발사하는 방법을 찾으려고 노력하고 있습니다.

function clearForm()
{
$(':input','#calcQuery')
.not(':button, :submit, :reset, :hidden')
.val('');
};
// form popup 
$(document).ready(function() 
{
//var dataString = $("#calcQuery").serialize();
    $("#formBox").dialog({
      bgiframe: true,
        autoOpen: false, 
        height: 600,
        width: 400, 
        modal: false,
        closeOnEscape: true,
        title: "Calculator",
        buttons:    {
            "Calculate": function() {

// form post
            $.ajax({
            type: "POST",
            url: "calc.php",
            data: $("#calcQuery").serialize(),
            dataType: "html",
            success: function(response)
                {
                $("#calcBox").html(response);
                $("#calcBox").show();   
                },
            error: function
                (xhr, ajaxOptions, thrownError)
                    {
                    alert(xhr.status); 
                    alert(thrownError);
                    }



    }).responseText;

// form post 

                }
            } 
    });

$('#calcButton').click(function(){
    $('#formBox').dialog('open');
    return false;
    });

});

$("#formBox").bind('dialogclose', function(event)
{
clearForm();
}); 
도움이 되었습니까?

해결책 3

나는 그것을 사용하여 일하게했다 ...

function clearForm(form)
{
    $(":input", form).each(function()
    {
    var type = this.type;
    var tag = this.tagName.toLowerCase();
        if (type == 'text')
        {
        this.value = "";
        }
    });
};

그리고 .....

// form post
            $.ajax({
            type: "POST",
            url: "calc.php",
            data: $("#calcQuery").serialize(),
            dataType: "html",
            success: function(response)
                {
                $("#calcBox").html(response);
                $("#calcBox").show();   
                clearForm("#calcQuery");
                },
            error: function
                (xhr, ajaxOptions, thrownError)
                    {
                    alert(xhr.status); 
                    alert(thrownError);
                    }



    }).responseText;

// form post

... 지금 .. 라디오 버튼을 "GB"의 기본값으로 어떻게 설정합니까?

&nbsp;KB <input type="radio" name="curr_unit" value="KB" />
&nbsp;MB <input type="radio" name="curr_unit" value="MB" />
&nbsp;GB <input type="radio" name="curr_unit" value="GB" checked/>
&nbsp;TB <input type="radio" name="curr_unit" value="TB" />

감사해요

다른 팁

이것은 양식을 재설정합니다.

$("#form").trigger( "reset" );

사용 닫기 이벤트

$("#formBox").dialog({
      bgiframe: true,
        autoOpen: false, 
        height: 600,
        width: 400, 
        modal: false,
        close: clearForm
});

더 우아한 방법의 조합입니다.

...
beforeClose: function() {
    $("#form").trigger("reset");
},
...

저장에서 양식을 재설정, 취소 및 제목 바 닫기 버튼에서 재설정합니다. 수동으로 수행 해야하는 Select2의 예외 :

$("#mySelect2").select2('data', null);

같은 beforeclose 내부

`jQuery("#form").trigger( "reset" );`

성공 기능에 배치하십시오 ~ 후에 성공 메시지가 표시되었습니다.
그런 다음 완벽하게 작동합니다!
예시:

success : function(){
jQuery('#contactsMsgs').html('<p class="success">All is well, your e&ndash;mail has been sent.</p>');
jQuery('#yourformname').trigger( 'reset' );
spinner.hide();`
} 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top