简体   繁体   中英

How to get properties of Kendo element in javascript

I've got several Kendo NumericTextBoxes on a Razor view page and a event handlers for them:

@Html.Kendo().NumericTextBoxFor(model => model.SignedNumericTextBox1).Step(0.25f).Events(e => e.Change("ChangeSignedNumericTextBox1"))
@Html.Kendo().NumericTextBoxFor(model => model.SignedNumericTextBox2).Step(0.25f).Events(e => e.Change("ChangeSignedNumericTextBox2"))
@Html.Kendo().NumericTextBoxFor(model => model.SignedNumericTextBox3).Step(0.25f).Events(e => e.Change("ChangeSignedNumericTextBox3"))

<script>
function ChangeSignedNumericTextBox1() {
    var val = this.value()
    alert(this.name);
    if (val > 0) {
        $("#SignedNumericTextBox1").kendoNumericTextBox({ format: "+##.##", decimals: 2 });
    }
    else {
        $("#SignedNumericTextBox1").kendoNumericTextBox({ format: "##.##", decimals: 2 });
    }
}

function ChangeSignedNumericTextBox2() {
    var val = this.value()
    alert(this.name);
    if (val > 0) {
        $("#SignedNumericTextBox2").kendoNumericTextBox({ format: "+##.##", decimals: 2 });
    }
    else {
        $("#SignedNumericTextBox2").kendoNumericTextBox({ format: "##.##", decimals: 2 });
    }
}

function ChangeSignedNumericTextBox3() {
    var val = this.value()
    alert(this.name);
    if (val > 0) {
        $("#SignedNumericTextBox3").kendoNumericTextBox({ format: "+##.##", decimals: 2 });
    }
    else {
        $("#SignedNumericTextBox3").kendoNumericTextBox({ format: "##.##", decimals: 2 });
    }
}
</script>

Is there a way to reference a numeric text box in my JavaScript so that I only need to have one ChangeSignedNumericTextBox function?

This should hopefully help:

<script>
  function changeme()
  {

    var myId = '#' + this._form.context.id;
    var val = $(myId).val();

    if (val > 0) {
         $(myId).kendoNumericTextBox({ format: "+##.##", decimals: 2 });
    }
    else {
         $(myId).kendoNumericTextBox({ format: "##.##", decimals: 2 });
    }

  }

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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