在jquery中使用变量获取字段值

时间:2014-09-28 05:45:19

标签: javascript jquery

我需要从表中获取字段值。我得到了字段ID,但是当我得到该字段的值时,它给出了错误。我不知道这个。我已经搞砸了,但没有变量的例子。谁能帮助我在这里获得价值呢?!以下是我的尝试:

    function setTotalAmount(id){
             var rateField = id.replace(".AMOUNT", ".RATE"),
             rateSelector = "'#"+rateField+"'",
             rate = $(rateSelector).val();
             alert(rate)
        }

这是我的HTML ::

first template >>

    <script type="text/javascript">
    function setTotalAmount(id){
        var rateField = id.replace(".AMOUNT", ".RATE"),
                rateSelector = "#"+rateField,
                rate = $(rateSelector).val();
        alert(rate)
    }

    var childCount = ${up_Proc_MasterInstance?.upDirectProcurementDetails?.size()} + 0;
    $('.delButton').live('click', function() {
//find the parent div
        var prnt = $(this).parents(".detail-div");
//find the deleted hidden input
        var delInput = prnt.find("input[id$=deleted]");
        delInput.attr('value', 'true');
//hide the div
        prnt.hide();
    });
    function addChild() {
//debugger;
        var clone = $("#detail_clone").clone();
        var htmlId = 'upDirectProcurementDetails[' + childCount + '].';

        clone.find("input[id$=id]").attr('id', htmlId + 'id').attr('name', htmlId + 'id');
        clone.find("input[id$=deleted]").attr('id', htmlId + 'deleted').attr('name', htmlId + 'deleted');
        clone.find("input[id$=new]").attr('id', htmlId + 'new').attr('name', htmlId + 'new').attr('value', 'true');

        clone.find("input[id$=RATE]").attr('id', htmlId + 'RATE').attr('name', htmlId + 'RATE');
        clone.find("input[id$=AMOUNT]").attr('id', htmlId + 'AMOUNT').attr('name', htmlId + 'AMOUNT');
        clone.find("input[id$=TOTAL_PRICE]").attr('id', htmlId + 'TOTAL_PRICE').attr('name', htmlId + 'TOTAL_PRICE');

        clone.attr('id', 'detail' + childCount);

        $("#detailList").append(clone);
        clone.show();
        childCount++;
    }
</script>
<div class="" style="border: solid 1px;border-radius: 5px">
    <table  style=" border-radius:5px;table-layout: fixed" id="detailList" class="table table-bordered table-striped table-hover table-condensed">
        %{--<table id="example" class="dataListTable table table-bordered table-striped table-hover table-condensed">--}%
        <tr><th>ITEM ID</th><th>ITEM NAME</th><th>ITEM DETAILS</th><th>RATE</th><th>AMOUNT</th><th>TOTAL AMOUNT</th><th width="44px">Action</th></tr>
        <g:render template='detail' model="['result':null,'i':'_clone','hidden':true]"/>
        <g:each var="upDirectProcurementDetails" in="${up_Proc_MasterInstance.upDirectProcurementDetails}" status="i" >
            <g:render template='detail' model="['upDirectProcurementDetails':upDirectProcurementDetails,'i':i]"/>
        </g:each>
    </table>
    <input type="button"  style="border: 2px solid" class="btn btn-info btn-md save" value="Add Detail" onclick="addChild();"/>
</div>

第二个模板,其中只有行&gt;&gt;

 <tr id="detail${i}" name="tr[${i}]" class="detail-div" <g:if test="${hidden}">style="display:none;"</g:if>>
    <g:hiddenField name='upDirectProcurementDetails[${i}].id' value='${upDirectProcurementDetails?.id}'/>
        <g:hiddenField name='upDirectProcurementDetails[${i}].deleted' value='false'/>
        <g:hiddenField name='upDirectProcurementDetails[${i}].new' value="${upDirectProcurementDetails?.id == null?'true':'false'}"/>

<td><g:textField size="13px" name='upDirectProcurementDetails[${i}].RATE' value='${upDirectProcurementDetails?.RATE}' class='detail-number form-control'/></td>
<td><g:textField size="13px" name='upDirectProcurementDetails[${i}].AMOUNT' value='${upDirectProcurementDetails?.AMOUNT}' onblur="setTotalAmount(this.id)" class='detail-txt form-control'/></td>
<td><g:textField size="13px" name='upDirectProcurementDetails[${i}].TOTAL_PRICE' value='${upDirectProcurementDetails?.TOTAL_PRICE}' class='detail-txt form-control' readonly=""/></td>
<td><span style="font-size: 20px;color: #ff0000"  class='glyphicon glyphicon-trash delButton' id='delButton${i}' name='results[${i}].delButton'/></td>
</tr>

2 个答案:

答案 0 :(得分:0)

使用rateSelector = "#"+rateField,

使用var rateField = id.replace("#AMOUNT", "#RATE"),

你不需要将它包装在单个引号中。

答案 1 :(得分:0)

使用以下代码。我想这可能会对你有所帮助。

function setTotalAmount(){
var rateField = $('#AMOUNT').attr('id','RATE');
rate = $(rateField).val();
alert(rate)
}
setTotalAmount();