简体   繁体   中英

Fetch input value after Dropdown menu item selected

I have a table about products. It has id, productdmc, productcode columns. In this select menu, productdmc is showing. When one item was selected, label its gonna change with related rows. Thats what i need. But i cant figure out the solution.

productcode .

<select class="form-control" name="productdmc" id="productdmc">
    <option disabled selected>DMC</option>
    @foreach ($product as $products)
    <option value="{{ $products->productdmc }}">{{ $products->productdmc }}</option>
    @endforeach
    </select>

Related input

<input type="text" name="productcode" id="productcode">

This is the js code. I dont know this is working. This is my actual problem.

<script type="text/javascript">
$(document).ready(function() {
    $('select[name="productdmc"]').on('change', function() {
        var tmp = $(this).val();
        if(tmp) {
            $.ajax({
                url: '/products/create/'+tmp,
                type: "GET",
                dataType: "json",
                success:function(data) {


                    $('productcode').empty();
                    $.each(data, function(key, value) {
                        $('productcode').innerHTML('<input value="'+ key +'">');
                    });


                }
            });
        }else{
            $('productcode').empty();
        }
    });
});
</script>

In my Controller:

$val = DB::table("products")->pluck("productdmc","productcode");
    return json_encode($val);

I know, i messed up so much. But codes are so complicated than this. I write this code in here(shorter version). Not copy-paste. I stucked here in a while. I cant find what is the real solution is. I am open for all kinda solution.

Sorry for my bad english.

innerHTML is a property of HTML elements, not a function of jQuery's representation.

Use html(content) instead, and I think it should work:

$('#productcode').html('<input value="'+ key +'">');

Your selector is wrong, you forgot the '#'.

var $productCode = $('#productcode');

Also, when the event 'change' is triggered, you need to fetch the selected option.

var selectedValue = $element.find('option:selected').val();


HTML

<select class="form-control" name="productdmc" id="productdmc">
  <option disabled selected>DMC</option>
  @foreach ($product as $products)
    <option value="{{ $products->productdmc }}">{{ $products->productdmc }}</option>
  @endforeach
</select>

<input type="text" name="productcode" id="productcode">

Javascript:

<script type="text/javascript">
$(document).ready(function() {
    $('body').on('change', 'select[name="productdmc"]', function(e) {
      var $element = $(e.currentTarget); // $(this) is also fine.
      // Get selected value
      var selectedValue = $element.find('option:selected').val();
      var $productCode = $('#productcode'); 

      // No value selected
      if (!selectedValue) {
        $productCode.val('');
        return;
      }

      $.ajax({
          url: '/products/create/' + selectedValue,
          type: 'GET',
          dataType: 'json',
          error: function() {
            $productCode.val('');
          },
          success: function(res) {
            $productCode.val('');

            if (res.length < 1) return;


            $productCode.val(res[0] || '');
            // This will populate more than 1 field. So,
            // I'm not sure about what you expect on the backend. 
            // If you want to populate more than 1, 
            // you should change the selector/field (do not use id in this case)

            // $.each(res, function(key, value) {
            //   $productCode.val(key);
            // });
         }
      })
    });
});
</script>

PHP

<?php

$products = DB::table('products')->get();

return response(
  $products->pluck('productdmc', 'productcode')->toArray()
);

You can read more about jQuery selectors here: https://api.jquery.com/category/selectors/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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