簡體   English   中英

獲取兩個輸入元素的值,然后檢查第三個值。 將值設置為第四元素

[英]Get the values of two input elements and check the value ot third. Set value to forth element

我有兩個輸入元素和一個選擇元素。 我想要的是獲取輸入元素的值並檢查選擇的值。 輸入元素是產品基本價格和價格標記,因此它們是十進制數字。

選擇值為1和2。1為固定價格,2為百分比。

因此,我想檢查基本價格輸入字段是否有值,價格標記字段是否有值並檢查選擇字段。

如果輸入字段不為空並且select = 1(固定價格),則將基礎價格和價格標記字段相加,並將結果設置為第4個字段的值,即“ price”。

如果選擇值為= 2(百分比),則獲得基准價格和價格標記中的+或-數字(視為百分比)。

到目前為止,我有以下代碼:

<div class="form-group">

            <label class="col-sm-2 control-label" for="input-base_price"><?php echo $entry_base_price; ?></label>

            <div class="col-sm-10">

              <input type="text" name="base_price" value="<?php echo $base_price; ?>" placeholder="<?php echo $entry_base_price; ?>" id="base_price" class="form-control" />

            </div>

          </div>

          <div class="form-group">

            <label class="col-sm-2 control-label" for="input-price_mark"><?php echo $entry_price_mark; ?></label>

            <div class="col-sm-10">

              <input type="text" name="price_mark" value="<?php echo $price_mark; ?>" placeholder="<?php echo $entry_price_mark; ?>" id="price_mark" class="form-control" />

            </div>

          </div>

          <div class="form-group">

            <label class="col-sm-2 control-label" for="input-price_mark_type"><?php echo $entry_price_mark_type; ?></label>

            <div class="col-sm-10">

              <select name="price_mark_type" id="price_mark_type" class="form-control">
                <option value="1"><?php echo $entry_fixed_price;?></option>
                <option value="2"><?php echo $entry_percent;?></option>
              </select>
            </div>

          </div>

          <div class="form-group">

            <label class="col-sm-2 control-label" for="input-price"><?php echo $entry_price; ?></label>

            <div class="col-sm-10">
            <script type="text/javascript">

            base_price = $("#base_price").attr('value');
            price_mark = $("#price_mark").attr('value');
            price_mark_type = $("#price_mark_type").attr('value');

            fixed = base_price + price_mark;
            percent = base_price * (price_mark / 100);

              if (base_price && price_mark == 1 ) {
                  $('#price').val(fixed);
              }
              if (base_price && price_mark == 2 ) {
                  $('#price').val(percent);
              }

            </script>
              <input type="text" value="" name="price" placeholder="<?php echo $entry_price; ?>" id="price" class="form-control" />

            </div>

          </div>

我也嘗試過:

<script type="text/javascript">

              var base_price = document.getElementById("base_price").value;
              var price_mark = document.getElementById("price_mark").value;

              var price_mark_type = document.getElementById("price_mark_type").value;



              if (base_price && price_mark == 1 ) {
                  document.getElementById("price").value = 'parceInt(base_price) + parceInt(price_mark)';
              }
              if (base_price && price_mark == 2 ) {
                  document.getElementById("price").value = 'parceInt(base_price) * (parceInt(price_mark) / 100)';
              }

            </script>

我嘗試了這些建議,但到目前為止沒有結果: 如何使用jQuery設置輸入文本的值

在輸入文本框中獲取值

如果您使用的是jQuery,
price_mark_type = $(“#price_mark_type”)。val()應該返回1或2。

base_price && price_mark == 1應該改為base_price && price_mark_type == 1

var base_price = $("#base_price").val();
            var price_mark = $("#price_mark").val();
            var price_mark_type = $("#price_mark_type").val();

            if (base_price && price_mark_type == 1) {
                $("#price").val(parseInt(base_price) + parseInt(price_mark));
            }
            else if (base_price && price_mark_type == 2) {
                $("#price").val(parseInt(base_price) + (parseInt(base_price) * (parseInt(price_mark) / 100)));
            }

工作於:

 $('#price_mark').click(function(){
            var base_price = $("#base_price").val();
            var price_mark = $("#price_mark").val();
            var price_mark_type = $("#price_mark_type").val();

            if (base_price && price_mark_type == 1) {
                $("#price").val(parseInt(base_price) + parseInt(price_mark));
            }
            else if (base_price && price_mark_type == 2) {
                $("#price").val(parseInt(base_price) + (parseInt(base_price) * (parseInt(price_mark) / 100)));
            }
            });

感謝jeetaz!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM