我正在尝试创建一个投票系统。每当用户填写带有标题和您可以投票的两个可能选项的表单时,数据就会使用 ajax 附加到页面上的 div 元素中。使用单选按钮,当用户单击两个可能的投票选项之一(一个输入值为 0,另一个值为 1)时,我想将值(0 或 1)发送到 php 文件,该文件然后将被插入到数据库中。
这是我单击输入字段时发生的情况: https ://jsfiddle.net/crdiling/dczup26h/36 HTML:
<div class='display-polls-container'>
</div>
<button id='user-created-poll'>Click for new poll</button>
Javascript:
$("#user-created-poll").click(function() {
$(".display-polls-container").append("<div class='poll-container'><form><div class='title'><p class='user'>Username</p><p class='title-text'>Poll title</p></div><label class='userPollLabel'><div class='inner-content'><input type='radio' name='polls' value='0'>First option</div></label><label class='userPollLabel'><div class='inner-content'><input type='radio' name='polls' value='1'>Second item</div></label></form></div>");
$("input:radio").click(function() {
var value = $(this).val();
alert(value);
});
});
//How do I make it so that when I click on one of the inputs, it only gives the value of the input that I'm clicking and not the value of all the inputs?
CSS:
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
button {
position: absolute;
top: 0;
right: 2%;
width: 25%;
height: 10%;
}
.poll-container {
border-style: solid;
}
我想要发生的是只返回一个值,但是当有多个轮询时,值 0 或 1 会多次返回。