javascript - 无法访问 AEM 中 javascript 内的 html 变量

标签 javascript aem

我试图制作一个以姓名和电子邮件作为输入的表单。单击提交按钮,它应该显示一个警告框,上面写着“谢谢您name”。

我知道如何通过设置 Id 来访问 HTML 中的输入变量并在脚本标记内访问它们,但 AEM 中出现了问题。这是我的代码

<sly data-sly-test="${wcmmode.edit}">
    <span>${component.title}</span>
</sly>

<div style="margin-bottom: 10px">
    <div>
        <label>${properties.namelabel}</label>
        <input
            type="${properties.inputType}"
            name="${properties.name}"
            placeholder="${properties.placeholder}"
            required="${properties.required && 'required'}"
            />
    </div>
     <div>
         <label>${properties.maillabel}</label>
         <input
            type="${properties.einputType}"
            name="${properties.ename}"
            placeholder="${properties.eplaceholder}"
            required="${properties.required && 'required'}"
           />
    </div>
    <div>
        <button type = "button" onclick="myFunction();">

            ${properties.buttlabel}

        </button>
    </div>

</div>

<script>
function myFunction() {
  alert("Thank You");
}
</script> 

如果我在 input 标签中设置 Id,我如何才能在 Javascript 中专门访问 name 变量?

最佳答案

评论回复和其他答案都没有解决我的问题。在尝试了一些 jquery/JS 组合之后,我可以通过在输入标记中添加 id 选择器来使其工作:

 <input id = "abc"
     type="${properties.inputType}"
     name="${properties.name}"
     placeholder="${properties.placeholder}"
     required="${properties.required && 'required'}"
  />

从脚本标签内的 id 获取名称值:

<script>
function myFunction() {

var nameValue = document.getElementById("abc").value;
  alert("Thank You" + nameValue);
}
</script>

关于javascript - 无法访问 AEM 中 javascript 内的 html 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54344537/

相关文章:

javascript - 如何追加部分 View 的根元素

aem - HTL/Sightly 是否可以有条件地打开或关闭标签?

java - 在 CQ5 中搜索多个标签

aem - 如何在 AEM 上的列表组件对话框中使用子编辑器?

javascript - 动态选择框在 IE7 中未正确居中

javascript - 如何在传单中查找相邻的多边形

Javascript 表单阻止在调用第二个函数时不起作用

javascript - 如何将大于两位数的数字传递给计算器 jQuery + HTML 中的数组

less - AEM 6.1 LESS 版本

aem - currentDesign.writeCssincludes 到底包含哪些内容?