简体   繁体   English

Kendo按钮在点击时调用javascript函数

[英]Kendo button calling a javascript function on click

I have a HTML input like this: 我有这样的HTML输入:

<input type="submit" name="saveForm" value="Save" onclick="return validateForm()"/>

here's the validateForm function called when the button is clicked: 这是单击按钮时调用的validateForm函数:

function validateForm() {
    if (someErrorCondition) {
        alert("error");
        return false;
    }
    reallySubmitForm();
}

I want to replace this button with a Kendo Button, and I'm doing like this: 我想用Kendo Button替换这个按钮,我这样做是这样的:

@(Html.Kendo().Button()
    .Name("save")
    .HtmlAttributes( new {type = "submit"})
    .ImageUrl(Url.Content("~/Images/save.png"))
    .Events(e => e.Click("return validateForm"))    <---------  here's the problem
    .Content("Save")
)

The problem is that, if I have 问题是,如果我有

.Events(e => e.Click("return validateForm"))

the button isn't rendered correctly: the "return" word is messing it up. 按钮显示不正确:“ return”一词将其弄乱了。 If I have this 如果我有这个

.Events(e => e.Click("validateForm"))

it's rendered correctly and the function is called. 正确渲染并调用该函数。 However, when clicked, the button submits the form anyway, without caring with validation. 但是,单击后,按钮无论如何都会提交表单,而无需进行验证。

How can I achieve the former behaviour with this Kendo Button? 如何使用此Kendo Button实现以前的行为?

Try to use e.preventDefault() instead of returning false in order to stop the submit event. 尝试使用e.preventDefault()而不是返回false来停止Submit事件。 That's the recommended way anyway. 无论如何,这是推荐的方式。 :) :)

I have tried with only " return " and it works. 我已经尝试过只有“ 返回 ”,它的工作原理。 But using e.preventDefault() does not do the trick for me. 但是使用e.preventDefault()并不能解决我的问题。

Try Following code: 尝试以下代码:

@(Html.Kendo().Button()
.Name("save")
.HtmlAttributes( new {type = "submit"})
.ImageUrl(Url.Content("~/Images/save.png"))
.Events(e => e.Click("validateForm"))
.Content("Save"))

function validateForm(e) {
  if (someErrorCondition) {
    alert("error");
    e.preventDefault();
  }
 else {
      reallySubmitForm();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM