duanre4421 2014-05-19 08:49
浏览 26
已采纳

jQuery在编辑表单页面上没有加载数据

I had some help with some jQuery code for a form to create a cascading style dropdown. Select option in first dropdown and displays the relevant data in second dropdown.

$(function () {
    window.all_options = $("[name='breed'] > option").detach(); // global variable

    $("[name='pet_type']").change(function () {
        var pet_type = $(this).val();
        $("[name='breed']").removeAttr("disabled").append(window.all_options);
        $("select[name='breed'] > option").each(function () {
            var o = $(this);
            if (o.data('pet-type') !== pet_type) {
            o.detach();
            }
        });
    });
});

The issue I am having is once the form is submitted, all data is inserted in to a database, and when I go to edit the form it should pull all data in relevant fields which is does for everything else except the second dropdown.

The first dropdown displays the correct pet_type which enables the second dropdown, but that is just empty with nothing.

Does the code need to be different or is it to do with something else?

Cheers

  • 写回答

1条回答 默认 最新

  • dongliantong3229 2014-05-19 09:12
    关注

    Well you have defined an onChange event listener with .change() but once the edit form is loaded, there is no actual onChange event happening (i guess your data comes pre-rendered in HTML). So you could trigger it manually by adding ("[name='pet_type']").trigger("change") to the end of your jQuery onLoad function.

    $(function () {
        window.all_options = $("[name='breed'] > option").detach(); // global variable
    
        $("[name='pet_type']").change(function () {
            var pet_type = $(this).val();
            $("[name='breed']").removeAttr("disabled").append(window.all_options);
            $("select[name='breed'] > option").each(function () {
                var o = $(this);
                if (o.data('pet-type') !== pet_type) {
                o.detach();
                }
            });
        });
    
        $("[name='pet_type']").trigger("change");
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办