jQuery Load form elements through ajax, from onchange event of drop down box

前端 未结 4 432
一生所求
一生所求 2020-12-07 05:14

I have a dropdown and need to dynamical load form elements based on a statement


                        
    
提交评论

  • 2020-12-07 05:55

    Following code gets the data via ajax call for OnChange Event and fills another DropDown

        $("#IdOfyourDropDown").change(function () {
    
          $.getJSON('<%= ResolveUrl("~/PutYourURL/?Id="1)%>', function (data) 
                {
                        Result = data; //Use this data for further creation of your elements.
                         var items = "";
                        items += "<option value=0> -- </option>";
                        $.each(data, function (i, SingleElement) {
                            items += "<option value='" + SingleElement.Value + "'>" + SingleElement.Text + "</option>";
                        });
                        $("#AnyOtherDropDown").html(items);
    
                });
         });
    

    I used getJSON to retrieve the data, you can use many more

    0 讨论(0)
  • 2020-12-07 05:55

    We may want to try this also

    $("#type").change(function() {
        $.post(
            "yourRequestHandlingPage.php",
            { 
              param: $(this).val();
            },
            function(data) {
                //supposing data holds the html output of dynamically creawted form element
                $(".myformcontent").append(data); //add the elements at the end of the form elemetn
            }
    });
    
    0 讨论(0)
  • 2020-12-07 05:56

    try this:

    $(function() {
    
      $('#type').bind('change', function(ev) {
    
         var value = $(this).val();
    
         $.ajax({
            ...
            data: {valueType: value, html: encodeURIComponent($("#addhtml").html())},
            ...
         });
    
      });
    
    
    });
    
    0 讨论(0)
  • 提交回复
    热议问题