我有一个引用表单,使用PHP从数据库中提取数据。从DB中提取的一个字段是合作伙伴级别(银色,金色,铂金)。
合作伙伴级别下面的字段显示合作伙伴根据合作伙伴获得的折扣%(更高级别等于更高折扣)。
加载表单并从数据库中提取合作伙伴级别后,我希望合作伙伴级别下方的字段根据用户的合作伙伴级别显示折扣。
以下是我目前设置的表单的字段集:
<fieldset>
<div class="form-top">
<div class="form-top-left">
<h2>Partner Information</h2>
<h3>Tell us who you are</h3>
</div>
<div class="form-top-right">
<img src="assets/images/icons/partner_info.png" class="form-icon">
</div>
</div>
<div class="form-bottom">
<div class="form-group">
<input type="text" readonly="readonly" name="form-partner-name" placeholder="Partner name" class="form-partner-name form-control" id="form-partner-name"
value="<?php echo $_SESSION['user']['firstName'], " ", $_SESSION['user']['lastName']?>">
</div>
<div class="form-group">
<input type="text" readonly="readonly" name="form-partner-level" placeholder="Partner Level" class="form-partner-level form-control" id="form-partner-level"
value="<?php echo $_SESSION['user']['partnerLvl']?>">
</div>
<div class="form-group">
<input type="text" readonly="readonly" name="discount-perc" placeholder="Discount(%)" class="form-control" id="form-discount-perc">
</div>
<div class="form-group">
<input type="text" name="form-margin-perc" placeholder="Margin(%)" class="form-control" id="form-margin-perc">
</div>
<br />
<button type="button" class="btn btn-next">Next</button>
</div>
</fieldset>
如何使用javascript读取合作伙伴级别字段的值来更新折扣字段?
答案 0 :(得分:0)
输入应为:
<input type="text" name="form-partner-level" placeholder="Partner level" class="form-partner-name form-control" id="form-partner-level" 'onblur="updateDiscount()"'
value="<?php echo $_SESSION['user']['partnerLvl']?>">
然后脚本是
<script language="javascript"><!--
function updateDiscount() {
var level = document.forms["your-form"].form-partner-level.value;
var discount = document.forms["your-form"].discount-perc.value;
if (level == "Platinum") {
discount = 20;
} else {
discount = something-else;
}
//--></script>
***更新:OP要求对页面加载进行计算。在这种情况下:
<?php
if ($_SESSION['user']['partnerLvl'] == "Platinum") {
$discount = 20;
} else {
$discount = something-else;
}
...
<input type="text" readonly="readonly" name="discount-perc" class="form-control" id="form-discount-perc" value="<?php echo $discount; ?>" />