简体   繁体   English

如何将数据从按钮传输到字段输入?

[英]How to transfer data from a button to a field input?

Good day!再会!

The site has a modal window with a form.该站点有一个带有表单的模式窗口。 And all over the site there are scattered buttons with a call to this window.整个网站上都有一些分散的按钮,可以调用此窗口。

How to transfer data to a hidden input field in a form in a modal window?如何将数据传输到模式窗口中表单中的隐藏输入字段?

An example of a form and a button:表单和按钮的示例:

<form id="37619" method="post">
            <input type="hidden" name="name" id="">
            <input type="text" class="form" name="is-phone" id="form-input-is-phone" placeholder="name">
            <input type="tel" class="form" name="is-phone" id="form-input-is-phone" placeholder="phone">

            <button class="button is-100">Test</button>
        </form>


        <button class="button" data-info="Data from a button or block">Send request</button>

PS Need clean javascript, without jQuery PS需要干净的javascript,没有jQuery

Thank you in advance :)先感谢您 :)

First give the hidden input field a unique id like hiddenField首先给隐藏的输入字段一个唯一的id,比如hiddenField

<input type="hidden" name="name" id="hiddenField">

and attach an onclick event to the button that should transfer data to the input field并将 onclick 事件附加到应该将数据传输到输入字段的按钮

<button class="button" data-info="Data from a button or block" onclick="populate(this);">Send request</button>

You're using the data attribute to store the string you'd like to transfer to the input field.您正在使用 data 属性来存储要传输到输入字段的字符串。 To retrieve the custom data stored inside data-info we need to access the .dataset property of the button.要检索存储在data-info 中的自定义数据,我们需要访问按钮的.dataset属性。

So to wrap it up:所以总结一下:

 function populate(element) { document.getElementById("hiddenField").value = element.dataset.info; }
 <form id="37619" method="post"> <input type="hidden" name="name" id="hiddenField"> <input type="text" class="form" name="is-phone" id="form-input-is-phone" placeholder="name"> <input type="tel" class="form" name="is-phone" id="form-input-is-phone" placeholder="phone"> <button class="button is-100">Test</button> </form> <button class="button" data-info="Data from a button or block" onclick="populate(this);">Send request</button>

Update:更新:

If you don't want to add a click listener using the onclick attribute you can achieve the same functionality via script.如果您不想使用onclick属性添加点击侦听器,您可以通过脚本实现相同的功能。 To do this give the button an unique id too要做到这一点,也给按钮一个唯一的 id

<button id="theButton" class="button" data-info="Data from a button or block">Send request</button>

Now we're able to get a reference to our button using现在我们可以使用

document.getElementById("theButton")

and add a click event listener并添加一个点击事件监听器

document.getElementById("theButton").addEventListener("click", populate);

The callback function - populate - is almost identical.回调函数 - populate - 几乎相同。 The difference is that in order to get the element that caused the click event we need to query the .target property of the event.不同的是,为了获取导致点击事件的元素,我们需要查询事件的.target属性。

 function populate(evt) { document.getElementById("hiddenField").value = evt.target.dataset.info; } document.getElementById("theButton").addEventListener("click", populate);
 <form id="37619" method="post"> <input type="hidden" name="name" id="hiddenField"> <input type="text" class="form" name="is-phone" id="form-input-is-phone" placeholder="name"> <input type="tel" class="form" name="is-phone" id="form-input-is-phone" placeholder="phone"> <button class="button is-100">Test</button> </form> <button id="theButton" class="button" data-info="Data from a button or block">Send request</button>

Details are commented in demo详情见demo

 // Collect all .modal-btn into a NodeList const btns = document.querySelectorAll('.modal-btn'); // Loop thru NodeList and register each <button> to click event for (let btn of btns) { btn.addEventListener('click', transferData); } // Pass event object through callback function function transferData(e) { // Call the function that controls modal getModal(); // Reference the hidden <input> const info = document.forms.modal.elements.info; /* Assign hidden <input> value to the [data-info] value of the clicked <button> (e.target always references the element that user interacted with -- clicked, changed, hovered, etc) */ info.value = e.target.dataset.info; // Verify the new value of hidden <input> console.log(info.value); } // The proceeding code is for demo purposes and is not required function getModal() { if (document.forms.modal) { document.forms.modal.remove(); } const node = document.querySelector('template').content const modal = node.cloneNode(true); document.querySelector('body').appendChild(modal); document.forms.modal.addEventListener('click', exitModal); } function exitModal(e) { if (e.target === this || e.target.name === 'cancel') { this.remove(); } }
 [name=modal] { width: 100vw; height: 100vh; position: fixed; top:0; left:0; z-index: 1; background: rgba(0,0,0,0.3); } fieldset { width: 50vw; height: 50vh; position:absolute; top: calc(50% - 25vh); left: calc(50% - 25vw); animation: fade 1.5s; background: #fff; text-align:center; } @keyframes fade { 0%{ opacity:0.0; } 100% { opacity: 1.0; } }
 <template> <form id="m37619" name='modal' method="post"> <fieldset name='fields'> <input name="info" type="hidden"> <input name="name" class="form" type="text" placeholder="Jon Doe"> <input name="phone" class="form" type="tel" placeholder="408-378-5555"><br> <button name='cancel' type='button'>Cancel</button> <button>Send</button> </fieldset> </form> </template> <button class="modal-btn" data-info="data1">1</button> <button class="modal-btn" data-info="data2">2</button> <button class="modal-btn" data-info="data3">3</button> <button class="modal-btn" data-info="data4">4</button> <button class="modal-btn" data-info="data5">5</button>

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

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