【问题标题】:Displaying Data Upon Data Retrieval - Laravel在数据检索时显示数据 - Laravel
【发布时间】:2017-11-19 05:20:57
【问题描述】:

我可以使用下面的代码将数据保存到我的数据库中。当用户单击复选框时,项目的名称将显示并存储在数据库中。但我现在的问题是,当我编辑存储的数据时,我能够检索已检查的项目(之前选择并保存的项目),但用于附加代码的 JS 没有运行。

当我取消选中并再次检查时,它可以正常工作。

编辑时,会检查保存在数据库中的项目,但显示所选项目名称的代码的 JS 部分没有运行。为什么会这样?

HTML

<div class="form-group">
  <label for="name" class="col-lg-1 control-label"> Name</label>
  <div class="col-lg-8">
    <input type="text" class="typeahead form-control" id="name" value="{!!$item->products['name']!!}" placeholder="  Name" name="name" required>
  </div>
</div>
<input onclick="return data(this)" data-food="{{$product->toJson()}}" type="checkbox" id="{!! $product->id !!}" name="{!! $product->name !!}" 
value="{!! $product->price !!}"  @foreach ($product->emps as $deliver)   @if($product->id == $deliver->id) checked @endif @endforeach  />

JS

function data(item)
{ 
  var ad = JSON.parse(item.dataset.stuff);

  if(item.checked == true)  {
    $('.container').append(
    '<div class="shipment_container" > '+ 
    '<p  class="name" >'+ad.name+'</p>'+
    '</div>');
  }
  else {
    $(".container.shipment_container [data-id=" + ad.id + "]").parent().remove();
  }
}

更新

$( document ).ready(function() {


    function data(item)
    { 
      var ad = JSON.parse(item.dataset.stuff);

      if(item.checked == true)  {
        $('.container').append(
        '<div class="shipment_container" > '+ 
        '<p  class="name" >'+ad.name+'</p>'+
        '</div>');
      }
      else {
        $(".container.shipment_container [data-id=" + ad.id + "]").parent().remove();
      }
    }



});

【问题讨论】:

  • 获取&lt;input&gt; 元素并将其传递给document ready 上的data() 函数。
  • @Camilo 请查看我的更新,但它仍然无法正常工作
  • 你能显示从刀片生成的 html 代码吗?
  • 1.检查是否在单击复选框时调用data函数(使用警告框) 2.如果它工作,请检查调用函数时是否存在容器alert($('.container').length)
  • @SupunFictionPraneeth 是点击时调用数据函数

标签: javascript jquery laravel


【解决方案1】:

您仅在单击复选框时调用data(),但在编辑屏幕上您需要在加载时调用它并手动传递参数。

类似这样的:

function data(item)
{ 
  if(item.checked == true)  {
    // Do something if the item was chacked
  }
  else {
    // Do something else if it wasn't
  }
}

$( document ).ready(function() {

  var input = document.querySelector('input#{!! $product->id !!}');

  data(input);
});

【讨论】:

  • 即使使用 document 就绪函数也不会发生任何事情。此外,代码中有很多inputs,所以我如何在这一行中指定var input =document.querySelector('input');
  • 您应该在输入中添加一个 ID 或适当的类并修改 querySelector()。调用data(input)ad的值是多少?
  • 是的,我有一个特定输入的 id。
  • 我在您的示例中没有看到任何 id 属性。
  • 这个input有id属性
猜你喜欢
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 2014-07-07
  • 2018-09-19
  • 1970-01-01
相关资源
最近更新 更多