Javascript 如何在使用SilverStripe的表单上有效地添加额外的自定义JQuery函数来提交按钮
我正在将SilverStripe 3.0与SilverStripe userforms子模块一起使用。 这真的很难解释,所以请耐心等待 我想在表单中添加一个自定义函数(check textarea word count),但是我不确定如何最好地附加该函数,因为我不想破坏核心验证js文件Javascript 如何在使用SilverStripe的表单上有效地添加额外的自定义JQuery函数来提交按钮,javascript,php,jquery,silverstripe,Javascript,Php,Jquery,Silverstripe,我正在将SilverStripe 3.0与SilverStripe userforms子模块一起使用。 这真的很难解释,所以请耐心等待 我想在表单中添加一个自定义函数(check textarea word count),但是我不确定如何最好地附加该函数,因为我不想破坏核心验证js文件 $('button[type="submit"]').entwine({ onclick: function () { var $this = $('textarea.max-words'
$('button[type="submit"]').entwine({
onclick: function () {
var $this = $('textarea.max-words');
var wordcount = getWords($this);
if (wordcount > maxWords) {
if ($this.next().is("span")) {
$('#maxwords-error').html(function () {
return spanText(wordcount, maxWords);
});
}
else {
$this.after(function () {
return "<span id='maxwords-error' class='required message'>" + spanText(wordcount, maxWords) + "</span>"
});
return false;
}
}
}
})
$('button[type=“submit”])。纠缠({
onclick:function(){
var$this=$('textarea.max words');
var wordcount=getWords($this);
如果(字数>最大字数){
如果($this.next().is(“span”)){
$('#maxwords error').html(函数(){
返回span文本(字数、最大字数);
});
}
否则{
$this.after(函数(){
返回“+spanText(字数,最大字数)+”
});
返回false;
}
}
}
})
Silverstripe使用了entwine库,所以我也尝试过这样做
我的问题是
如何让我的自定义函数在每次按下submit按钮时运行,与之前的字数正确与否无关。我认为您还应该取消click事件,以免其传播。类似这样的内容(简化):
这确保了一旦字数检查失败,事件管道的执行就会停止。如果所有表单字段(除字数外)都有效,这将阻止提交表单。谢谢,今晚我将对此进行测试。
$('button[type="submit"]').entwine({
onclick: function (evt) {
if(wordCountValidationFailed){
evt.preventDefault();
evt.stopPropagation();
evt.stopImmediatePropagation();
return false;
}
}
});