为什么 jquery 警报消息在 foreach 循环中无法准确工作


why jquery alert message not working accurately inside foreach loop?

当下拉列表框为空时,我正在尝试通过 jquery 在 foreach 循环中查看消息警报框,但没有工作怎么办?

我的蛋糕代码如下:

 <?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;
});

});

通过此代码,当我单击第一个记录按钮代码时,

代码工作,但当我单击不查看警报框消息时,其他记录按钮!

那是因为它被编程为仅响应第一个option

if (ddlist.val() == "") {
    //If the "Please Select" option is selected display error.
    alert("Please select a list data!");
    return false;
}

如果你想在每个option上显示你的消息(alert)(无论出于何种原因,你可以删除上面代码的if条件,所以你只得到:

//changed: always displays message now.
alert("You selected something in the list!");
return false;

相反。