0

I have a hidden field 'Edit-WorkOrderId'.

  function fn_formCreated(event, data) {
        $("#Edit-WorkOrderId").change(function() {
            if ($(this).val() == "") {
                $("#Edit-ProductionStageId option[value='-1']").attr("selected", "selected");
                $("#Edit-ProductionStageId").attr("disabled", true)
                    .closest("div.jtable-input-field-container")
                    .removeClass("mandatory");
            } else {
                $("#Edit-ProductionStageId").removeAttr("disabled")
                    .closest("div.jtable-input-field-container")
                    .addClass("mandatory");
                fn_GetWorkOrderDeatils();
            }
        });
        if (data.formType == "create") {
            $("#Edit-WorkOrderId").trigger("change");
            fn_GetBusinessUnit();
        }
    }

The change event did not worked when the value changed. Thanks Advance.

Karthik Arthik
  • 276
  • 2
  • 18

3 Answers3

2

You have to define the change event like below supposing Edit-WorkOrderId is the ID of the field

$('#Edit-WorkOrderId').on('change', function() {
    //do something;
});

function fn_formCreated(event, data) {
   // do something.
   if (data.formType == "create") {
        $("#Edit-WorkOrderId").trigger("change");
        fn_GetBusinessUnit();
   }
}
rrk
  • 15,677
  • 4
  • 29
  • 45
dpanshu
  • 453
  • 3
  • 14
1

You need to put the change binding in the $(document).ready(function{}) call.

$(document).ready(function () {
    $("#Edit-WorkOrderId").on('change',function () {
        if ($(this).val() == "") {
            $("#Edit-ProductionStageId option[value='-1']").attr("selected", "selected");
            $("#Edit-ProductionStageId").attr("disabled", true)
                .closest("div.jtable-input-field-container")
                .removeClass("mandatory");
        } else {
            $("#Edit-ProductionStageId").removeAttr("disabled")
                .closest("div.jtable-input-field-container")
                .addClass("mandatory");
            fn_GetWorkOrderDeatils();
        }
    });
})

function fn_formCreated(event, data) {
    if (data.formType == "create") {
        $("#Edit-WorkOrderId").trigger("change");
        fn_GetBusinessUnit();
    }
}
rrk
  • 15,677
  • 4
  • 29
  • 45
1

Try something like this:

$('#Edit-WorkOrderId').on('input change', function() {
  if (this.value === '') {
    alert('no value');
  } else {
    alert(this.value);
  }
}).change();

setTimeout(function() {
  $('#Edit-WorkOrderId').val('newValue in timeout.').trigger('change');
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='hidden' id='Edit-WorkOrderId' value=''>

You need to trigger the change whenever you are updating the value.

some notes:

  1. You are binding change event in a function.
  2. When you call this function it registers the change event only. does not fire it.
  3. You need to put your change event inside doc ready.
Jai
  • 74,255
  • 12
  • 74
  • 103