【问题标题】:Thymeleaf form allow input if radio checked如果选中无线电,Thymeleaf 表单允许输入
【发布时间】:2020-07-23 20:51:42
【问题描述】:

我希望仅在选中单选按钮返回时才允许用户在返回日期中输入输入。我已经在下面上传了我的表格,如果我需要添加任何其他内容,请告诉我。另外,当用户第一次进入表单时,如何不勾选单选按钮?

<div class = "col-sm-6 form-group">
                    <label>Return date: </label> 
                    <Input type="date" th:field="*{returnDate}"/>
                    </div>
                    <div class = "col-sm-6 form-group">
                    <label>Return </label>
                    <Input type="radio" th:field="*{flightReturn}"
                            th:value="true" />
                    <label>One-way </label>
                    <Input type="radio" th:field="*{flightReturn}"
                            th:value="false" />
                            </div>

编辑:使用了 javascript,它有所帮助

【问题讨论】:

  • 这与Spring 或Thymeleaf 无关。您可以简单地使用 javascript 或 jquery 来处理此功能。

标签: spring spring-boot spring-mvc thymeleaf


【解决方案1】:

您可以使用 jQuery 轻松实现这一点。

<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<script>
$( document ).ready(function() {
     $('.true-radio').click(function()
  {
    $('.date-input').removeAttr("disabled");
  });

  $('.false-radio').click(function()
  {
    $('.date-input').attr("disabled","disabled");
  });
});
</script>

您还需要在 html 中添加类:

<div class = "col-sm-6 form-group">
                    <label>Return date: </label> 
                    <Input type="date" class="date-input" th:field="*{returnDate}"/>
                    </div>
                    <div class = "col-sm-6 form-group">
                    <label>Return </label>
                    <Input class="true-radio" type="radio" th:field="*{flightReturn}"
                            th:value="true" />
                    <label>One-way </label>
                    <Input class="false-radio" type="radio" th:field="*{flightReturn}"
                            th:value="false" />
                            </div>

如果您将所有这些代码添加到您的 html 中,它应该可以工作。

【讨论】:

    猜你喜欢
    • 2016-11-19
    • 1970-01-01
    • 2012-06-06
    • 2015-12-20
    • 2016-03-30
    • 2017-10-20
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多