如何在启用和禁用表单提交之间切换?

时间:2015-05-22 21:30:37

标签: javascript jquery ajax forms

以下是瘦:在下面的代码<http auto-config="true" use-expressions="true" access-denied-page="/faces/forbidden.xhtml" entry-point-ref="casAuthEntryPoint"> <intercept-url pattern="/faces/parameters.xhtml" access="hasRole('TMS_ADMIN')" /> <intercept-url pattern="/faces/*" access="hasRole('TMS_USER')" /> <session-management invalid-session-url="adfasdf" > <concurrency-control error-if-maximum-exceeded="true" max-sessions="1"/> </session-management> </html> #new-org-btn类型为input,我希望它只提交包含submit的代码form click函数到达else语句。截至目前,即使它没有达到else声明也在提交。

$('#new-org-btn').click(function() {
    var newOrgName = $('#new-org-ipt').val();
    console.log(newOrgName.length);;
    if (newOrgName.length == 0)
    {
        $('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot be blank.");
    }
    else if (newOrgName.indexOf('-') == -1)
    {
        $('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot contain any '-' characters.");  
    }
    else
    {
        $('#new-org-output-msg').css('color', 'green');
        $('#new-org-form').submit();                
    }                            
});

我知道$('#new-org-form').submit(function() { return false; });会禁止表单在提交时执行任何操作,但问题是如果我这样做

$('#new-org-btn').click(function() {
    var newOrgName = $('#new-org-ipt').val();
    console.log(newOrgName.length);;
    if (newOrgName.length == 0)
    {
        $('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot be blank.");
        $('new-org-form').submit(function() { return false; });

    }
    else if (newOrgName.indexOf('-') == -1)
    {
        $('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot contain any '-' characters.");
        $('new-org-form').submit(function() { return false; });  
    }
    else
    {
        $('#new-org-output-msg').css('color', 'green');
        $('#new-org-form').submit();                
    }                            
});

然后,当我实际输入else语句并希望像往常一样提交表单时,它可能已从之前调用该函数中取消激活。因此,我需要一种方法来重新激活&#34;表格。我该怎么做?

2 个答案:

答案 0 :(得分:1)

由于#new-org-btn的类型为submit,因此无论如何都会提交。因此,您需要return falseif

中的else if
$('#new-org-btn').click(function() {
        var newOrgName = $('#new-org-ipt').val();
        console.log(newOrgName.length);;
        if (newOrgName.length == 0)
        {
            $('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot be blank.");
            return false;
        }
        else if (newOrgName.indexOf('-') == -1)
        {
            $('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot contain any '-' characters.");
            return false;  
        }
        else
        {
            $('#new-org-output-msg').css('color', 'green');
            //$('#new-org-form').submit(); not need if new-org-form is the form itself                
        }                            
    });

关于

&GT; ......需要一种“重新激活”表格的方法......

$('form')[0].reset();//may need to change selector if you have multiple forms

应该这样做。

请参阅How to reset a form using jQuery with .reset() method

答案 1 :(得分:1)

我会做的有点不同:

仅在提交时进行检查,而不是单击本身 - 这样做会更好,因为提交按钮和“输入”键都将被处理:

$('#form').submit(function(ev) {

        var newOrgName = $('#new-org-ipt').val();
        if (newOrgName.length == 0)
        {
            $('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot be blank.");
            ev.preventDefault();
        }
        else if (newOrgName.indexOf('-') == -1)
        {
            $('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot contain any '-' characters.");
            ev.preventDefault();
        }
        else
        {
            $('#new-org-output-msg').css('color', 'green');
        }                            
    });