简体   繁体   English

在表单中提交按钮数据

[英]Submitting button data in form

I am trying to understand how to implement the following concept:我试图了解如何实现以下概念:

What I am trying to implement我正在尝试实施的

A user can type in their username + room, and then click on a button to indicate the color they want.用户可以输入他们的用户名+房间,然后单击一个按钮来指示他们想要的颜色。

So far I have the username + room part working, it is passed in the URL as parameters.到目前为止,我的用户名 + 房间部分正在工作,它作为参数传入 URL。 But what if I wanted to pass (through URL params) which color button was pressed as well?但是,如果我也想通过(通过 URL 参数)按下哪个颜色按钮呢? Since the buttons aren't input elements, the data isn't passed through the URL, but I want it to be.由于按钮不是输入元素,因此数据不会通过 URL 传递,但我希望它是。 Here is what the form looks like so far:到目前为止,表单如下所示:

<form action="/reaction.html">
    <label>Choose a username:</label>
    <input type="text" name="username" placeholder="insert username" requried autocomplete="off">
    <label>Room name:</label>
    <input type="text" name='room' placeholder='insert room' required autocomplete="off">
    <label>Chose your color:</label>
    <div class="colors">
         <button type="button" class="buttonround buttonred" name="red"></button>
         <button type="button" class="buttonround buttonblue" name="blue"></button>
         <button type="button" class="buttonround buttongreen" name="green"></button>
         <button type="button" class="buttonround buttonyellow" name="yellow"></button>
    </div>
    <button class="submitbutton">Join</button> 
</form>

To make it clear right now I get something like: "http://localhost:3000/reaction.html?username=a&room=a" when submitting the form, I want to find a way to add a param at the end, maybe something like "?color=red".为了清楚起见,我在提交表单时得到类似:“http://localhost:3000/reaction.html?username=a&room=a”,我想找到一种在最后添加参数的方法,也许类似“?颜色=红色”的东西。

I know I probably need to attach onclick listeners for each button, but what to do after listening for a click?我知道我可能需要为每个按钮附加 onclick 监听器,但是在听到点击后该怎么办?

If you want to go with button approach only, try to create a hidden input and set the value on click event of color button如果您只想使用按钮方法 go,请尝试创建隐藏输入并设置颜色按钮单击事件的值

<form action="/reaction.html">
    <label>Choose a username:</label>
    <input type="text" name="username" placeholder="insert username" requried autocomplete="off">
    <label>Room name:</label>
    <input type="text" name='room' placeholder='insert room' required autocomplete="off">
    <label>Chose your color:</label>
    <input type="hidden" name="color" id="color">
    <div class="colors">
         <button type="button" class="buttonround buttonred" data-value="red">Red</button>
         <button type="button" class="buttonround buttonblue" data-value="blue">Blue</button>
         <button type="button" class="buttonround buttongreen" data-value="green">Green</button>
         <button type="button" class="buttonround buttonyellow" data-value="yellow">Yellow</button>
    </div>
    <button type="submit" class="submitbutton">Join</button> 
</form>

Script part脚本部分

$('.buttonround').click(function(e) {
    $('#color').val($(this).data('value'));
});

Other possible way is to create a custom radio button.其他可能的方法是创建一个自定义单选按钮。 Please refer this code for radio button which looks like a button.请参阅此代码以获取看起来像按钮的单选按钮。 https://codepen.io/salfx/pen/ZXOVjo https://codepen.io/salfx/pen/ZXOVjo

Here is a working snippet.这是一个工作片段。 You will need to remove the ev.preventDefault();您将需要删除ev.preventDefault(); to actually allow the submission of the form after the join button is pressed.在按下join按钮后实际允许提交表单。

 const frm=document.querySelector('form'); frm.addEventListener('click',function(ev){ if (ev.target.tagName=='BUTTON') { if (ev.target.className=='sbutton') { ev.preventDefault(); // for testing... console.log([...frm.querySelectorAll('input')].map(el=>[el.name,el.value])); } else frm.color.value=ev.target.name; } });
 input {display:block}.buttonround {width:40px; height:40px; border-radius:20px; border:none}.buttonred{background-color:red}.buttonblue{background-color:blue}.buttongreen{background-color:green}.buttonyellow{background-color:yellow}
 <form action="/reaction.html"> <label>Choose a username:</label> <input type="text" name="username" placeholder="insert username" requried autocomplete="off"> <label>Room name:</label> <input type="text" name='room' placeholder='insert room' required autocomplete="off"> <input type="hidden" name="color"> <label>Chose your color:</label> <div class="colors"> <button type="button" class="buttonround buttonred" name="red"></button> <button type="button" class="buttonround buttonblue" name="blue"></button> <button type="button" class="buttonround buttongreen" name="green"></button> <button type="button" class="buttonround buttonyellow" name="yellow"></button> </div> <button class="sbutton">Join</button> </form>

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

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