【问题标题】:Depending on Select option change others value on div and input field in Laravel根据 Select 选项更改 Laravel 中 div 和输入字段上的其他值
【发布时间】:2021-12-24 22:45:48
【问题描述】:

我正处于学习期。我需要一个解决方案,但我找不到。

我有一个动态选择选项。我想要这个,当改变选择选项时,它将在另一个div中显示数据库中的所有相关列。

数据库表名:提供

列名:id、姓名、详情、充值

控制器:

$offers = Offer::all();
return view('sim_sale', compact('offers'));

刀片文件:

<select name="offer_id" id="offer" class="form-control">
    <option value=""> Select Offer</option>
    @foreach ( $offers as $row )
    <option value="{{ $row->id }}">{{ $row->name }}</option>
    @endforeach
</select>
 <div id="offer-details">
     {{$offers->details}
 </div> 
 <input type="text" value="{{$offers->recharge}}" id="offer-recharge" />

这里,如果选择选项发生变化,它将显示详细信息和充值,否则这些字段将不显示任何内容。此外,用户可以在输入字段中输入数据。

请帮我解决。

【问题讨论】:

  • 那你要研究一下 AJAX 是怎么做的
  • 在选项上添加一个 id 并添加一个不显示的类(d-none 用于引导)如果事件被触发(tbf 没有线索,但可能类似于 onclick)你应该能够做一个在 id 上删除类
  • 我想,我不需要添加或删除类。在 `{{$offers->details}}` 和 `{{$offers->recharge}}` 中,值将根据选择 ID 显示。如果select id为3,则显示第3个id的详细信息,从数据库中充值列数据。我认为ajax建议是完美的。但是我不知道如何编写ajax代码。你能帮忙吗?

标签: javascript php jquery jquery-selectors laravel-7


【解决方案1】:

我已经找到了解决方案。 这是我的代码:

 <select name="offer_id" id="parent_id" class="form-control dynamic" data-dependent="details">
       <option value=""> Select Offer</option>
       @foreach ( $offers as $row )
       <option value="{{ $row->id }}">{{ $row->name }}</option>
       @endforeach
       </select>
       @foreach($offers as $row)  
       <div class="some"  id="some_{{ $row->id }}"  style="display:none;">
        {{ $row->details }}
       </div>
       @endforeach

<script type="text/javascript">
$('#parent_id').on('change',function(){
$(".some").hide();
var some = $(this).find('option:selected').val();
$("#some_" + some).show();}); </script>
                            

【讨论】:

    猜你喜欢
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多