如何绑定在thymeleaf下降?

时间:2018-06-04 20:16:57

标签: java ajax spring thymeleaf

我正在尝试从下拉列表中绑定枚举。无论选择什么,它只传递第一个选项,在本例中为OPEN。

我也使用AJAX来传递它。

HTML表单

sourceSets {
    main.res.srcDirs =  [
                         'src/main/res/anim',
                         'src/main/res/color',
                         'src/main/res/drawable',
                         'src/main/res/drawable-hdpi',
                         'src/main/res/drawable-mdpi',
                         'src/main/res/drawable-xhdpi',
                         'src/main/res/drawable-xxhdpi',
                         'src/main/res/drawable-xxxhdpi',
                         'src/main/res/menu',
                         'src/main/res/mipmap-dpi',
                         'src/main/res/mipmap-hdpi',
                         'src/main/res/mipmap-mdpi',
                         'src/main/res/mipmap-xhdpi',
                         'src/main/res/mipmap-xxhdpi',
                         'src/main/res/mipmap-xxxhdpi',
                         'src/main/res/raw',
                         'src/main/res/values',
                         'src/main/res/values-land',
                         'src/main/res/values-large',
                         'src/main/res/values-v21',
                         'src/main/res/valuesw820dp',
                         'src/main/res/xml',
                         'src/main/res/layout',
                         'src/main/res']
}

的Javascript

<form class="form-inline" id="search-form2" th:object="${stockTransaction}">
    <input type="text" class="input-small" id="symbol" placeholder="Symbol"/>
    <input type="text" class="input-small" id="shares" placeholder="Amount Of Shares"/>
    <input type="text" class="input-small" id="price" placeholder="Price of Shares"/>
    <select>
        <option th:id="state"
                th:each="state : ${T(com.example.demo.StockTransaction.State).values()}"
                th:value="${state}" th:text="${state}">
        </option>
    </select>
    <button type="submit" id="bth-search2" class="btn">Post Transaction</button>
</form>

模型

function stock() {

    var data = new Object();
    data.symbol = $('#symbol').val()
    data.numOfShares = $('#shares').val();
    data.priceOfShares = $('#price').val();
    data.state = $('#state').val();

    $("#btn-search2").prop("disabled", true);

    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "/post",
        data: JSON.stringify(data),
        dataType: 'json',
        cache: false,
        timeout: 600000,
        success: function (data) {
            $('input[type="text"],textarea').val('');
            profits();
            transactions();
            realizedProfit();
        },
        error: function (e) {
            console.log("ERROR : ", e);
        }
    });
}

控制器(无论选择什么,标准输出都会显示OPEN选择):

@Entity
public class StockTransaction {

    @Id
    @GeneratedValue
    private Long id;
    private int numOfShares;
    private int sharesInLot;
    private State state;

    public static enum State {
        OPEN, IN_WORK, FINISHED
    }

    getters/setters
}

请建议。

0 个答案:

没有答案