html - 表单只有 10 个按钮,但用户只能选择 1 个
问题描述
我试图制作一个带有标题、段落和满分 10 分的表格。我想让只有一个按钮进入数据库。我怎样才能做到这一点并保留我的按钮的当前样式?
例子
用户选择一个按钮
按钮 1
button 2 //用户在点击时选择这个按钮
用户更改选定按钮
button 1 //用户在点击时改变选中的按钮
按钮 2
.
当我点击提交时,它会将按钮 1 发送到数据库
代码:
<form method="POST" action="index.php?action=sendReview" class="redaction" enctype="multipart/form-data">
<div id="scores">
<input class="score" type="button" name="options" style="color:red;" value="1" id="option1">
<input class="score" type="button" name="options" style="color:red;" value="2"id="option2">
<input class="score" type="button" name="options" style="color:red;" value="3"id="option3">
<input class="score" type="button" name="options" style="color:orange;" value="4" id="option4">
<input class="score" type="button" name="options" style="color:orange;"value="5"id="option5">
<input class="score" type="button" name="options" style="color:orange;" value="6"id="option6">
<input class="score" type="button" name="options" style="color:orange;" value="7"id="option7">
<input class="score"type="button" name="options" style="color:green;" value="8"id="option8">
<input class="score" type="button" name="options" style="color:green;" value="9" id="option9">
<input class="score" type="button" name="options" style="color:green;" value="10" id="option10">
</div>
<input type="text"name="nameReview" class="nameReview"placeholder="Review Title">
<textarea id="story" name="review-content"rows="5" cols="33" placeholder="Write your Review here"></textarea>
<input type="submit" value="Publish" name="review" class="publish">
</form>
它看起来如何
解决方案
You can put your value in a hidden input. On submit of the form you can get this value by the name of the hidden field.
See below example, It will post userScore
as a user selected score.
$(".score").click(function(){
var btnId = $(this).val();
$("#userScore").val(btnId);
console.log($("#userScore").val());
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST" action="index.php?action=sendReview" class="redaction" enctype="multipart/form-data">
<div id="scores">
<input class="score" type="button" name="options" style="color:red;" value="1" id="option1">
<input class="score" type="button" name="options" style="color:red;" value="2"id="option2">
<input class="score" type="button" name="options" style="color:red;" value="3"id="option3">
<input class="score" type="button" name="options" style="color:orange;" value="4" id="option4">
<input class="score" type="button" name="options" style="color:orange;"value="5"id="option5">
<input class="score" type="button" name="options" style="color:orange;" value="6"id="option6">
<input class="score" type="button" name="options" style="color:orange;" value="7"id="option7">
<input class="score"type="button" name="options" style="color:green;" value="8"id="option8">
<input class="score" type="button" name="options" style="color:green;" value="9" id="option9">
<input class="score" type="button" name="options" style="color:green;" value="10" id="option10">
</div>
<input type="hidden" name="userScore" id="userScore">
<input type="text"name="nameReview" class="nameReview"placeholder="Review Title">
<textarea id="story" name="review-content"rows="5" cols="33" placeholder="Write your Review here"></textarea>
<input type="submit" value="Publish" name="review" class="publish">
</form>
推荐阅读
- azure-pipelines - 在类似于 MS 托管配置的 prem 代理上设置
- postgresql - 如何在不丢失openproject数据的情况下将postgresql数据库从10升级到12
- c# - WebRequest 异步 asp.net-mvc
- android - 函数在不应该被调用时被调用
- javascript - Foreach 仅显示 Angular echarts 中数组的最后一项
- amazon-web-services - 从同一 AWS 账户中的其他 EC2 实例启动/停止特定 EC2 实例
- sql - 使用 Azure DevOps 的多租户数据库部署
- javascript - 使用从 JSON 数据中提取的 URL 在 HTML 中设置 backgroundImage
- jquery - Bulma CSS 框架中的下拉实时搜索
- react-native - React Native 获取数组 json 数据