以下是瘦:在下面的代码<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;表格。我该怎么做?
答案 0 :(得分:1)
由于#new-org-btn
的类型为submit
,因此无论如何都会提交。因此,您需要return false
和if
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
应该这样做。
答案 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');
}
});