【发布时间】:2016-02-04 01:17:39
【问题描述】:
我正在通过模式提交表单。根据用户输入,需要将一些数据发送到模态。我需要在加载模式之前验证数据。如果没有选择订单,当前代码会阻止显示模态,但是一旦用户选择了一些订单并重新提交表单,模态仍然不显示。
JS
function batchHoldModalLauncher($hold_name){
// get modal id
var modal_id = "#"+$hold_name+"_modal_id";
// check if any orders are selected
if ($("#order_hold_table_body input:checkbox:checked").length > 0)
{
$(modal_id).modal('show');
}
else
{
// no boxes checked
$('.modal').on('show.bs.modal', function() {
return false;
});
alert('Choose some orders to put on hold');
}
}
提交表单并调用模态的Laravel PHP代码
<div class="col-md-3" style="margin-top: 20px;">
{!! Form::open(['url' => '/hold', 'method' => 'POST', 'class' => 'form-inline']) !!}
<h4>
Orders for Batch {{ $batch->id }}
<div class="btn-group">
<button type="button" class="btn btn-sm btn-danger dropdown-toggle" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
Hold <span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-danger">
@foreach($holds as $hold)
<?php $hold_name = str_replace(' ', '', $hold->name); ?>
<li><a href="#" data-toggle="modal" onclick="batchHoldModalLauncher('{{ $hold_name }}')"
data-target="#modal{{ $hold_name }}">{{ $hold->name }}</a></li>
@endforeach
</ul>
</div>
</h4>
<!-- Show each order in batch and allow user to place orders on hold -->
<table class="table table-striped" id="order_hold_table">
<thead>
<th>Order Number</th>
<th>Hold
<!-- de/select all checkboxes -->
{!! Form::checkbox('hold_toggle', null, false, [
'class'=>'toggle-button',
'style'=>'float:right'
]) !!}</th>
</thead>
<tbody id="order_hold_table_body">
@foreach($orders as $o)
<tr>
<td>
@if($type == 'receiving')
{{ $o->id }}
@elseif($type == 'sales')
{{ $o->order_number }}
@endif
</td>
<td>
{!! Form::checkbox('hold[]', $o->id, false, ['style'=>'float:right']) !!}
</td>
</tr>
@endforeach
</tbody>
</table>
{!! Form::close() !!}
【问题讨论】:
-
您的浏览器控制台有什么错误吗?
-
试试
console.log($hold_name)和console.log($("#order_hold_table_body input:checkbox:checked").length)看看你是否得到了你所期望的。 -
if块已按预期运行。刚刚使用 console.log 语句进行了测试。 -
当重新提交表单并选择订单时,您是否仍然收到警报,或者您只是收到“无”。那么它是否满足 if 语句?
-
你有小提琴吗?因为我明白,当一个复选框被选中时,在控制台中长度属性返回的东西 > 0?
标签: javascript jquery twitter-bootstrap laravel-5 bootstrap-modal