【问题标题】:cant save radio button selections to database with thymeleaf无法使用百里香将单选按钮选择保存到数据库
【发布时间】:2023-03-13 08:59:01
【问题描述】:

我有表单,里面有单选按钮选项,我有控制器类和模型。我可以成功保存其他数据,但单选按钮选项始终反映到数据库中为 0。我认为百里香单选按钮的实现有问题。

<form id="add" role="form" th:action="@{/add}" method="post" th:object="${radiobutton}">
    <input type="hidden" th:field="*{id}"/>

    <div class="form-group">
        <label>is it new</label>
        <label class="radio-inline">
            <input type="radio" name="optionsRadiosInline1" id="optionsRadiosInline1"
                   value="true" th:checked="*{isNew}"/>Yes
        </label>
        <label class="radio-inline">
            <input type="radio" name="optionsRadiosInline1" id="optionsRadiosInline2"
                   value="false" th:checked="*{isNew} == false"/>No
        </label>
    </div>

</form>

【问题讨论】:

    标签: thymeleaf


    【解决方案1】:

    我找到了,值应该是0或1,而不是真假

    <form id="add" role="form" th:action="@{/add}" method="post" th:object="${radiobutton}">
        <input type="hidden" th:field="*{id}"/>
    
        <div class="form-group">
            <label>is it new</label>
            <label class="radio-inline">
                <input type="radio" name="optionsRadiosInline1" id="optionsRadiosInline1" value="1"
                       th:checked="*{isNew}"/>Yes
            </label>
            <label class="radio-inline">
                <input type="radio" name="optionsRadiosInline1" id="optionsRadiosInline2" value="0"
                       th:checked="*{isNew} == false"/>No
            </label>
        </div>
    
    </form>
    

    【讨论】:

    • 我很幸运。经过2小时的血汗发现。感谢分享。投票!
    猜你喜欢
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 2012-12-30
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多