在jQuery中在计算值的末尾附加%?

时间:2015-07-17 15:03:41

标签: javascript jquery calculated-field

我有一个计算几个值并吐出成绩的表单。我希望这个等级可以提供。它们已经以整数形式出现,即:100.00。

var acqAmount1 = Number(jQuery("#edit-submitted-acquisition-amount-1").val().replace(/,/g,""));
    var acqAmount2 = Number(jQuery("#edit-submitted-acquisition-amount-2").val().replace(/,/g,""));
    var acqTotal = 0;

    if (acqAmount1) {
        acqAmount1 = parseFloat(acqAmount1);
    } else {
        acqAmount1 = 0;
    }

    if (acqAmount2) {
        acqAmount2 = parseFloat(acqAmount2);
    } else {
        acqAmount2 = 0;
    }

    if (acqAmount1 > 0 && acqAmount2 > 0) {
        acqTotal = ((acqAmount2 - acqAmount1) / acqAmount1 * 100).toFixed(2);
    } else {
        acqTotal = 0;
    }

    jQuery("#edit-submitted-acquisition-percent-change").val( acqTotal );

    grade += getAcquisitionPoints(acqTotal);




<fieldset>
        <h2 class="fs-title">Create your account</h2>
        <h3 class="fs-subtitle">This is step 2</h3>
            <!-- Begin Total Number of Donors in Year 1 Field -->
                <div class="form-item webform-component webform-component-textfield hs_total_number_of_donors_in_year_1 field hs-form-field" id="webform-component-acquisition--amount-1">

                    <label for="edit-submitted-acquisition-amount-1 total_number_of_donors_in_year_1-99a6d115-5e68-4355-a7d0-529207feb0b3_6344">What was your number of total donors in year 1?</label><span class="required"> * </span>

                    <input id="edit-submitted-acquisition-amount-1" class="form-text hs-input" name="submitted total_number_of_donors_in_year_1" required="required" size="60" maxlength="128" type="number" value="" placeholder="" data-rule-required="true" data-msg-required="Please enter a valid number">
                    <span class="error1" style="display: none;">
                        <i class="error-log fa fa-exclamation-triangle"></i>
                    </span>
                </div>
            <!-- End Total Number of Donors in Year 1 Field -->

            <!-- Begin Total Number of Donors in Year 2 Field -->
                <div class="form-item webform-component webform-component-textfield hs_total_number_of_donors_in_year_2 field hs-form-field" id="webform-component-acquisition--amount-2">

                    <label for="edit-submitted-acquisition-amount-2 total_number_of_donors_in_year_2-99a6d115-5e68-4355-a7d0-529207feb0b3_6344">What was your number of total donors in year 2?</label><span class="required"> * </span>

                    <input id="edit-submitted-acquisition-amount-2" class="form-text hs-input" name="submitted[acquisition][amount_2] total_number_of_donors_in_year_2" required="required" size="60" maxlength="128" type="number" value="" placeholder="" data-rule-required="true" data-msg-required="Please enter a valid number">
                    <span class="error1" style="display: none;">
                        <i class="error-log fa fa-exclamation-triangle"></i>
                    </span>
                </div>
                <!-- End Total Number of Donors in Year 2 Field -->

            <!-- Begin Calc of Total Number of Donors Fields -->
            <!-- THIS FIELD IS NOT EDITABLE | GRAYED OUT -->
                <div class="form-item webform-component webform-component-textfield webform-container-inline hs_total_donor_percent_change field hs-form-field">

                    <label for="edit-submitted-acquisition-percent-change total_donor_percent_change-99a6d115-5e68-4355-a7d0-529207feb0b3_6344">Total Donors Percent Change</label><span class="required"> * </span>

                    <input id="edit-submitted-acquisition-percent-change" class="form-text hs-input" name="total_donor_percent_change" readonly="readonly" size="60" maxlength="128" type="number" value="" placeholder="0"><span class="field-suffix">%</span>
                </div>
                <!-- End Calc of Total Number of Donors Fields -->
        <input type="button" data-page="2" name="previous" class="previous action-button" value="Previous" />
        <input type="button" data-page="2" name="next" class="next action-button" value="Next" />
        <div class="explanation btn btn-small modal-trigger" data-modal-id="modal-3">What Is This?</div>
    </fieldset>

我尝试了+= '%' + '%'的几种方法,但似乎无法做到正确。

1 个答案:

答案 0 :(得分:0)

更改目标input元素:

type="number" 

要:

type="text"

应该解决问题。使用:

jQuery("#edit-submitted-acquisition-percent-change").val( acqTotal + '%' );