普通网友 2015-12-04 18:41
浏览 21

检查所选选项是否唯一

I'm working on school project and I have a problem with validating select boxes. It's very simple trip planner and let's say that I have 3 places that user visited and now he has the option to determine the order of visits. For example: [1]cinema, [3]concert,[2]shopping mall. The select boxes are generated automatically by PHP. How can I write universal jQuery script that validates the select boxes that each of them is unique?

Here is PHP code that generates the select fields;

    $title = $_GET['title'];
    $elements = count($_GET['eventid']);
    echo "<h5>Order your trip</h5>";
    foreach($_GET['eventid'] as $index => $id)
        {
        echo"<strong>" .$title[$index] . "</strong><select name='".$id."'>";
            for($i=1;$i<=$elements;$i++){
                echo"<option value='". $i ."'>". $i ."</option>";
            }
        echo"</select><br><br>";
        }

Now I would like to validate select boxes no matter how many events there are. I came up with this code. It works but can it be done easier?

    $('#validate').click(function() {
    var selectValues = new Array();
    $('select.order').each(function() {
        selectValues.push($(this).val());
    });
    var elements = selectValues.length;
    var i;
    for (i = 0; i < elements - 1; i++) {
        if (selectValues[i] == selectValues[i + 1]) {
            alert('error');
        }
    }
});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 simulink单相桥式整流电路
    • ¥35 问问51单片机流水灯的代码该怎么写
    • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
    • ¥15 stata webuse报错
    • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
    • ¥15 如何利用AI去除图片中的竹架子
    • ¥15 python 写个基金爬取的代码,自动卖出功能
    • ¥15 Linux系统启动不起来
    • ¥15 为什么运行仿真数码管不亮(语言-c语言)
    • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导