javascript - 如何用jquery检测循环中的onchange

标签 javascript jquery onchange

这可能之前已经被问过,或者这个解决方案可能有一个简单的答案,但我目前陷入困境。我有这个表格,可以在 http://jsfiddle.net/DS73u/ 看到,用户可以使用唯一的名称以 3 种不同的方式输入坐标。还可以有多个入口,以便用户可以向表单添加更多输入。我循环遍历每个选中的复选框输入并将值存储到数组 2D 数组中。它存储它是什么方案和坐标值的位置。因此,用户输入 55.67484 和 -86.7685,它们将存储到数组中,并且可以通过 temp[0][0] = 55.67484 AND temp[0][1] = -86.7685 访问。

完成后,我想根据入口分离每组坐标。我将二维数组存储到对象属性中,其中 ent_name 是属性名称。尽管我在根据入口名称拆分值时遇到问题,但我不知道如何比较它,因为所有值都包含在循环内。也许onchange()或change()可以在jquery中工作?文本字段的值永远不会改变...

$(function() {

    /*
        This is where the magic happens when you click
        the "Preview Map" button
        It does several things...

        1.Loops through every checkbox in the #entrances
        2.Check to see if Entrance name has changed and store it into object with  array of coordinates
        2.Checks to see if the checkbox is checked
            a.Yes
                1.We see what checkbox is being checked
                to determine how we want to output the data
                inputted(Hence the switch)
                2.We grab the data from the form and store it
                into an array called coordinates
            b.No
                1.Do nothing
    */

    $('#prev_map').click(function() {

        //Make sure coordinates is empty before proceeding
        coordinates = {};
        temp = [];
        var ent_name;

        //console.log($('#entrances input:checkbox'));
        $('#entrances input:checkbox').each(function(){

            if (this.checked) {
                    //console.log($(this).parent('#coords').prevAll('input').val());
                    //coordinates.push($(this).parent('#coords').prevAll('input').val());
                    ent_name = $(this).parent('#coords').prevAll('input').map(function() {
                        console.log(ent_name);
                    }).get().change(function() {
                        console.log("We changed");
                    });
                    /*
                    ent_name = $(this).parent('#coords').prevAll('input').map(function() {
                        return $(this).val();
                    }).get();


                    console.log("Entrance Name: " + ent_name);
                    if (temp_name != ent_name) {
                        coordinates[temp_name] = temp;
                    } else {
                        temp = [];
                    };
                    var temp_name = ent_name;
                    console.log("Temporary Name: " + temp_name);*/

                switch(this.name) {

                    case "dec_coord":
                        temp.push($(this,'input').next().children('input').map(function() {
                            return $(this).val();
                        }).get());
                        break;
                    case "deg_coord":
                        var temp2 = $(this,'input').next().children("input,select").map(function() {
                            return $(this).val();
                        }).get().join(";");
                        //console.log(temp);
                        temp2 = temp2.toString();
                        temp2 = temp2.replace(";","°");
                        temp2 = temp2.replace(";","'");
                        temp2 = temp2.replace(";","\"");
                        temp2 = temp2.replace(";",",");
                        temp2 = temp2.replace(";","°");
                        temp2 = temp2.replace(";","'");
                        temp2 = temp2.replace(";","\"");
                        temp.push(temp2.split(","));
                        break;
                    case "utm_coord":
                        temp.push($(this,'input').next().children('input,select').map(function() {
                            return $(this).val();
                        }).get().join(","));
                        break;
                }

            } else {
                //console.log("wrong");
            };



        });

        $('#dialog').dialog({
            width: 500,
            height: 400,
            open: function() {
                //loadMap();
            }
        });
    });
    /*
            This is the replication of the Entrance Fields
            We will limit the number of entrances to 5
        */

        var template = $('#entrances .ent_clone:first').clone();
            var cloneCount = 0;

            var addEntrance = function(){
                cloneCount++;
                var entrance = template.clone().find(':input').each(function() {
                    var newID = this.id+cloneCount;
                    //$(this).prev().attr('for', newID);
                    this.id = newID;
                }).end()
                .attr('id', 'ent' + cloneCount)
                .appendTo("#ent");
            };
            $('#addEnt').click(addEntrance);
});

最佳答案

我没有关注这里的所有内容,但听起来您的问题是如何最好地存储您的信息,其中每个入口都有一组经纬度数据。

如果 ent_name 确实是一个您不希望更改的唯一键,则可能是一个 javascript 对象:

var myEntrances = {};
...
myEntrances[ent_name] = temp;

关于javascript - 如何用jquery检测循环中的onchange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5875434/

相关文章:

python - javascript 中的 OnChange 或 OnBlur 事件在 Django 中运行 python 函数并返回结果?

ajax - 简单的 jQuery AJAX 问题

javascript - 与桌面上的 Mega 导航交互时会触发 Mouseleave 事件

javascript - IE9 中远程文件的 window.print() 问题

javascript - 将按下的键发送到文件

jquery - 以编程方式选择和加载 jquery 选项卡

jquery - 解决IE7 bug "input type=file"onchange 触发两次?

jquery - HTML 表格 onChange

javascript - 使用 jQuery 我想在 TD 悬停时突出显示表中的列

javascript - 如何验证基于选项卡的列表表单并使用 php ajax 保存?