【问题标题】:How to add radio/checkbox data into form with other fields?如何将单选/复选框数据添加到其他字段的表单中?
【发布时间】:2017-01-03 20:55:00
【问题描述】:

我是 Jquery 的新手,我不知道如何在我的函数中添加几个带有其他数据类型(文本、数字等)的单选框/复选框。我正在使用 Java/Spring,它们有几个时刻:.jsp 文件、spring:message 等。请帮助我!坦克。 这是我的代码:

<div class="modal fade" id="editRow">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h2 class="modal-title"><spring:message code="company.add"/></h2>
            </div>
            <div class="modal-body">
                <form:form class="form-horizontal" method="post" id="detailsForm">
                    <input type="hidden" id="id" name="id">

                    <div class="form-group">
                        <label for="name" class="control-label col-xs-3"><spring:message code="insurance.tableName"/></label>

                        <div class="col-xs-9">
                            <input type="text" class="form-control" id="name" name="name" placeholder="Name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="description" class="control-label col-xs-3"><spring:message code="insurance.tableDescription"/></label>

                        <div class="col-xs-9">
                            <input type="text" class="form-control" id="description" name="description"
                                   placeholder="Description">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="franchise" class="control-label col-xs-3"><spring:message code="insurance.tableFranchise"/></label>

                        <div class="col-xs-9">
                            <input type="number" class="form-control" id="franchise" name="franchise"
                                   placeholder="1000">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="population" class="control-label col-sm-3"><spring:message code="insurance.tablePopulation"/></label>

                        <div class="radio-inline">
                            <input type="radio" class="col-xs-2" id="population" name="population" value="Kiev"><spring:message code="insurance.kiev"/><br/>
                            <input type="radio" class="col-xs-2" id="population" name="population" value="Kiev district"><spring:message code="insurance.kievDistrict"/><br/>
                            <input type="radio" class="col-xs-2" id="population" name="population" value="more than 1 million"><spring:message code="insurance.moreOneMillion"/><br/>
                            <input type="radio" class="col-xs-2" id="population" name="population" value="to 1 million"><spring:message code="insurance.toOneMillion"/><br/>
                        </div>
                        <div class="radio-inline">
                            <input type="radio" class="col-xs-2" id="population" name="population" value="to half of 1 million"><spring:message code="insurance.toHalfAMillion"/><br/>
                            <input type="radio" class="col-xs-2" id="population" name="population" value="more than 100 thousand"><spring:message code="insurance.moreOneHundred"/><br/>
                            <input type="radio" class="col-xs-2" id="population" name="population" value="to 100 thousand"><spring:message code="insurance.toOneHundred"/><br/>
                            <input type="radio" class="col-xs-2" id="population" name="population" value="other"><spring:message code="insurance.other"/><br/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="engine_power" class="control-label col-xs-3"><spring:message code="insurance.tableEngine_Power"/></label>

                        <div class="radio-inline">
                            <input type="radio" class="col-xs-2" id="engine_power" name="engine_power" value="more than 3.0"><spring:message code="insurance.engineThree"/><br/>
                            <input type="radio" class="col-xs-2" id="engine_power" name="engine_power" value="more than 2.0"><spring:message code="insurance.engineTwo"/><br/>
                            <input type="radio" class="col-xs-2" id="engine_power" name="engine_power" value="more than 1.6"><spring:message code="insurance.engineOne"/><br/>
                            <input type="radio" class="col-xs-2" id="engine_power" name="engine_power" value="to 1.6"><spring:message code="insurance.engineToOne"/><br/>>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="amount" class="control-label col-xs-3"><spring:message code="insurance.tableAmount"/></label>

                        <div class="col-xs-9">
                            <input type="number" class="form-control" id="amount" name="amount"
                                   placeholder="1296">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-offset-3 col-xs-9">
                            <button type="submit" class="btn btn-primary"><spring:message code="insurance.save"/></button>
                        </div>
                    </div>
                </form:form>
            </div>
        </div>
    </div>
</div>

我的功能:

var form;
function save() {
    form = $('#detailsForm')
    $.ajax({
        type: "POST",
        url: ajaxUrl,
        data: form.serialize(),
        success: function () {
            $('#editRow').modal('hide');
            updateTable();
            successNoty('Saved');
        }
    });
}

【问题讨论】:

  • 您是否尝试使用jQuery 来获取radio 的值并选择元素?
  • 或将自定义表单元素添加到其他元素?比如带有多选复选框的选择框之类的?
  • 不,我没有。我需要在一个表单中使用这些单选框/复选框以及名称、描述等其他字段。我不知道如何从单选框/复选框获取数据并插入我的#detailsForm。有人可以帮我吗?

标签: javascript jquery spring


【解决方案1】:

我修复了它,我只是将radio 选项替换为select-&gt;option 在我的代码中使用不同的值,它工作正常。

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    相关资源
    最近更新 更多