简体   繁体   English

如何在更改事件上设置切换输入字段的名称属性

[英]How to set the name attribute of a toggled input field on change event

I am trying to change the attribute name="" value of an input field that is toggled once a check box is set and a toggled input is shown and filled out.我正在尝试更改输入字段的属性name=""值,一旦设置了复选框并显示并填写了切换的输入,该输入字段就会切换。

User checks a check box, once the check box is checked an input field shows where the title once was.用户选中一个复选框,一旦选中该复选框,输入字段就会显示标题曾经所在的位置。 I got this part covered.我得到了这部分。

Once the input field shows, I want to change the name attribute in that input field with the value the user inputs all before submission of form.输入字段显示后,我想用用户在提交表单之前输入的值更改该输入字段中的 name 属性。

Here is the code I have tried...这是我尝试过的代码...

 document.getElementById('custom-check-box').addEventListener('click', function() { if (this.checked) { document.getElementById('custom-span').innerHTML = '<input type="text" id="customInputName" name="customInputName" placeholder="Name the custom attribute">'; } else { document.getElementById('custom-span').innerHTML = 'Custom: '; } }); // this bit of code is not working as intended. I want to get the input // value after the user changes or focus out of the input and then have that // value input into the name attribute for the same input. document.getElementById('customInputName').addEventListener('change', function() { if (this.onChange) { let value = document.getElementById('customInputName').value; this.setAttribute("name", value); } });
 <div title="'.$title.'" class="input-info"> <span id="custom-span">Custom:</span> Yes: <input id="custom-check-box" type="checkbox" class="input-check" name="custom" value="yes"> </div>

Error: Cannot read property 'addEventListener' of null错误: Cannot read property 'addEventListener' of null

I think it is because the input is not set yet in the DOM.我认为这是因为输入尚未在 DOM 中设置。 Should I set the input in HTML, hide it and then change the name attribute and then show it?我应该在 HTML 中设置输入,隐藏它然后更改名称属性然后显示它吗?

Any help would be greatly appreciated as I am a bit of a beginner with JavaScript.任何帮助将不胜感激,因为我是 JavaScript 的初学者。

Thank you in advance!先感谢您!

SEE ACCEPTED ANSWER FOR UPDATE ON WORKING CODE.有关工作代码的更新,请参见接受的答案。 -> Second snippit was added an edit to @CertainPerformance code that works better for my use. -> 第二个片段添加了对@CertainPerformance 代码的编辑,它更适合我的使用。

I'd create the <input> outside of any of the handlers, and give it the listener which assigns its value to its name.我会在任何处理程序之外创建<input> ,并给它一个监听器,将它的值分配给它的名字。 When the checkbox is checked, append the input to the container, otherwise clear the container:当复选框被选中时,append 将输入到容器中,否则清除容器:

 const input = document.createElement('input'); input.name = 'customInputName'; input.placeholder = 'Name the custom attribute'; const customSpan = document.getElementById('custom-span'); document.getElementById('custom-check-box').addEventListener('click', function() { if (this.checked) { customSpan.textContent = ''; customSpan.appendChild(input); } else { customSpan.textContent = 'Custom: '; } }); input.addEventListener('change', function() { input.name = input.value; });
 <div title="'.$title.'" class="input-info"> <span id="custom-span">Custom:</span> Yes: <input id="custom-check-box" type="checkbox" class="input-check" name="custom" value="yes"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM