在jQuery中计算值的末尾添加%



我有一个表格,计算一些值并吐出一个分数。我希望分数是1分。它们已经是整数了,例如: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>

我已经尝试了+= '%' + '%'的几种方法,但似乎不能得到正确的

将目标input元素改为:

type="number" 

:

type="text"

应该可以解决这个问题。用途:

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

相关内容

  • 没有找到相关文章

最新更新