通过Placeholder属性值获取元素

问题描述 投票:0回答:1

我试图在附加了一些动态表单的页面上找到一个输入元素。页面上的动态表单部分是根据帮助主题下拉列表中选择的选项加载的。

// Function to find the Betreff input element
    function findBetreffInputElement() {
        // Use a CSS selector to directly target the input element with the specified attributes
        var betreffInputElement = document.querySelector('input[placeholder="Betreff"]');
        return betreffInputElement;
    }

    // Event listener for changes in the Help Topic dropdown
    document.getElementById('topicId').addEventListener('change', function() {
        // Check if the selected Help Topic is "FuxNoten"
        if (this.value === '1') {
            // Find the Betreff input element when "FuxNoten" is selected
            var betreffInputElement = findBetreffInputElement();
            if (betreffInputElement) {
                // Do something with the input element, such as storing its ID or value
                console.log("Stored Betreff input ID:", betreffInputElement.id);
            } else {
                console.log("No Betreff input element found."); // Log if no input element is found
            }
        }
    });

但它说:没有找到 Betreff 输入元素,尽管它存在

javascript html jquery
1个回答
0
投票

如果没有 HTML,我们只能猜测出了什么问题。当我仅添加带有指定占位符的简单输入时,它就可以工作。所以我们看不到你做错了什么。

// Function to find the Betreff input element
function findBetreffInputElement() {
    // Use a CSS selector to directly target the input element with the specified attributes
    var betreffInputElement = document.querySelector('input[placeholder="Betreff"]');
    return betreffInputElement;
}

console.log(findBetreffInputElement());
<input placeholder="Betreff" />

© www.soinside.com 2019 - 2024. All rights reserved.