【问题标题】:jQuery hidden input value on change doesn't trigger更改时的jQuery隐藏输入值不会触发
【发布时间】:2018-06-13 07:15:14
【问题描述】:

Concrete5 有一个图像选择器,它在选择图片后使用图片 ID 更新隐藏的输入值,例如:

<input name="pictureID" value="22" type="hidden">

选择图像后,我需要将所选图像加载到“添加块”表单上。也就是说,在隐藏的输入更新为 ID 之后加载图像(我可以通过 ID 获取图像 URL)。

这仅适用于之前已选择并保存图像的情况:

$('input[name=pictureID]').on('change', function() {
...
}).trigger('change');

但是如果清除了图像选择器并选择了新图像,则上述方法不起作用,因为隐藏输入是在选择图像后动态添加的。好吧,好吧,我尝试了这个:

$(document).on('change', 'input[name=pictureID]', function() {
...
}).trigger('change');

但这也不起作用。可能是因为必须触发隐藏元素的更改才能获得新值。如果我自己更改值,我会触发它。但是,如果我首先需要知道系统何时更改了隐藏的输入值更改,我该如何触发呢?

如何在更新隐藏输入值时加载图像?

【问题讨论】:

    标签: jquery triggers onchange hidden concrete5-8.x


    【解决方案1】:

    这是在选择文件时触发的 javascript 代码
    +
    我添加了一个函数(handleCustomImageChoice),它接收所选文件的所有数据。

    $(document).ready(function(){
        function handleCustomImageChoice(result){
            console.log(result);
    
            //check the result object to get the url of the file
            //EXAMPLES :
            //get thumbnail url =
            console.log(result.files[0].resultsThumbnailImg);
    
            //get full size url =
            console.log(result.files[0].url);
        }
    
        $(document).on('change', 'form', function(){
            //when a file is selected in the file manager, then the nearest form is triggered to 'change'...
            //first check if the form that is triggered has the required input
            let closestForm = $('input[name=pictureID]').closest('form').attr('action');
            let currentForm = $(this).attr('action');
    
            if(closestForm == currentForm){
                //here your action when the hidden input value is changed
                console.log($('input[name=pictureID]').val());
                let fileID = $('input[name=pictureID]').val();
                if(fileID != 0){
                    // ConcreteFileManager.getFileDetails(fileID , callback);
                    ConcreteFileManager.getFileDetails(fileID, handleCustomImageChoice);
                }
            }
        });
    });
    

    在我解决这个问题的研究过程中,我还遇到了一个名为“ConcreteEvent”的 javascript 对象...该对象从核心处理 JS 事件...但是我无法订阅“FileManagerSelectFile”事件。所以我不能给你那个代码......但是上面的代码应该给你你需要的东西。

    【讨论】:

      猜你喜欢
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 2011-10-21
      • 1970-01-01
      相关资源
      最近更新 更多