当下拉列表框为空时,我试图在foreach循环中通过jquery查看消息提醒框但不能正常工作我该怎么办?
我的cakephp代码如下:
<?php
foreach ($data['customer'] as $index => $d):
$customer = $d;
$package = array();
if (count($data['package']) > 0) {
$package = $data['package'][$index];
}
?>
<tr class="odd gradeX">
<td><?php echo $customer['first_name'] . ' ' . $customer['middle_name'] . ' ' . $customer['last_name']; ?></td>
<td>
<ul>
<li>Cell:<?php echo $customer['cell']; ?></li>
<li>Address:<?php echo $customer['address'] ?></li>
</ul>
</td>
<td>
<?php if (count($package) > 0): ?>
<ul>
<li> Package Name: <?php echo $package['name']; ?></li>
<li> Month: <?php echo $package['duration']; ?></li>
<li> Charge: <?php echo $package['charge']; ?></li>
</ul>
<?php
endif;
?>
</td>
<td>
<?php
echo $this->Form->create('PackageCustomer', array(
'inputDefaults' => array(
'label' => false,
'div' => false
),
'id' => 'form_sample_3',
'class' => 'form-horizontal',
'novalidate' => 'novalidate',
'url' => array('controller' => 'admins', 'action' => 'changeservice')
)
);
?>
<?php
echo $this->Form->input('id', array(
'type' => 'hidden',
'value' => $customer['id']
)
);
?>
<?php
echo $this->Form->input('status', array(
'type' => 'select',
'id' => 'ddlist',
'options' => Array('ticket' => 'Generate Ticket', 'payment' => 'Customer Information', 'history' => 'Ticket History'),
'empty' => 'Select Action',
'class' => 'form-control form-filter input-sm',
)
);
?>
<br>
<?php
echo $this->Form->button(
'Go', array('class' => 'btn blue', 'id' => 'btnddlist', 'title' => 'Do this selected action', 'type' => 'submit')
);
?>
<?php echo $this->Form->end(); ?>
</td>
</tr>
<?php
endforeach;
?>
我的jquery代码是:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnddlist").click(function () {
var ddlist = $("#ddlist");
if (ddlist.val() == "") {
//If the "Please Select" option is selected display error.
alert("Please select a list data!");
return false;
}
return true;
});
});
通过此代码,当我点击第一个记录按钮代码工作但其他人记录按钮时,我点击不查看警报框消息!
答案 0 :(得分:1)
这是因为它被编程为仅响应第一个option
:
if (ddlist.val() == "") {
//If the "Please Select" option is selected display error.
alert("Please select a list data!");
return false;
}
如果您想在每个option
上显示您的消息(提醒)(无论出于何种原因,您只需删除上述代码的if
条件,这样您就得到了:
//changed: always displays message now.
alert("You selected something in the list!");
return false;
代替。