简体   繁体   English

获取两个输入元素的值,然后检查第三个值。 将值设置为第四元素

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

I have two input elements and one select element. 我有两个输入元素和一个选择元素。 What I want is to get the values of the input elements and check the value of the select. 我想要的是获取输入元素的值并检查选择的值。 the input elements are product base price and price-mark up so they are decimal numbers. 输入元素是产品基本价格和价格标记,因此它们是十进制数字。

The select is with values 1 and 2. 1 is Fixed price and 2 is Percentage. 选择值为1和2。1为固定价格,2为百分比。

So, I want to check if the base price input field has a value and the price mark field has a value and check the select field. 因此,我想检查基本价格输入字段是否有值,价格标记字段是否有值并检查选择字段。

If the input fields are not blank and the select = 1 ( fixed price ), then sum the base price and the price mark fields and set the result as a value of the 4th field, which is "price". 如果输入字段不为空并且select = 1(固定价格),则将基础价格和价格标记字段相加,并将结果设置为第4个字段的值,即“ price”。

If the select value is = 2 (percentage) then get the base price and + or - the number in price mark ( considered as percentage). 如果选择值为= 2(百分比),则获得基准价格和价格标记中的+或-数字(视为百分比)。

So far I have the below code: 到目前为止,我有以下代码:

<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>

I also tried and : 我也尝试过:

<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>

I tried these suggestions but no result so far: How to set value of input text using jQuery 我尝试了这些建议,但到目前为止没有结果: 如何使用jQuery设置输入文本的值

Get the value in an input text box 在输入文本框中获取值

If you're using jQuery, 如果您使用的是jQuery,
price_mark_type = $("#price_mark_type").val() should return 1 or 2. price_mark_type = $(“#price_mark_type”)。val()应该返回1或2。

base_price && price_mark == 1 should be instead base_price && price_mark_type == 1 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)));
            }

Worked with: 工作于:

 $('#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)));
            }
            });

Thanks to jeetaz! 感谢jeetaz!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM