在 jQuery 中的计算值末尾附加 %?
Appending a % on the end of a calculated value in jQuery?
我有一个表格可以计算一些值并给出一个等级。我想让成绩成为一个分数。它们已经以整数形式出现,即: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 + '%' );
我有一个表格可以计算一些值并给出一个等级。我想让成绩成为一个分数。它们已经以整数形式出现,即: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 + '%' );