选择下拉菜单项后获取输入值

时间:2018-01-18 12:15:11

标签: javascript ajax database laravel eloquent

我有一张关于产品的表格。它有id,productdmc,productcode列。 在此选择菜单中,显示 productdmc 当选择一个项目时,标记它将随相关行标记。这就是我需要的东西。但我无法弄清楚解决方案。

产品代码

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

这是js代码。我不知道这是有效的。这是我的实际问题。

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

在我的控制器中:

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

我知道,我搞砸了很多。但是代码比这复杂得多。我在这里写了这个代码(更短的版本)。不是复制粘贴。我在这里坚持了一段时间。我找不到真正的解决方案是什么。 我愿意接受各种解决方案。

抱歉我的英文不好。

2 个答案:

答案 0 :(得分:1)

innerHTML是HTML元素的属性,而不是jQuery表示的函数。

请改用html(content),我认为它应该有效:

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

答案 1 :(得分:1)

你的选择器错了,你忘记了#&#39;。

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

此外,当事件发生变化时,&#39;触发后,您需要获取所选选项。

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()
);

您可以在此处阅读有关jQuery选择器的更多信息:https://api.jquery.com/category/selectors/

相关问题