阵推按一下按钮的jQuery [英] array pushing on button click jquery

查看:219
本文介绍了阵推按一下按钮的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提高一些JavaScript我写的元素推到一个数组,如果他们不是pviously $ P $的数组中为止。然后,我想在一个选择栏显示的数组。我有这一切都在JavaScript工作,但认为这将是一个有趣的练习为自己尝试jQuery中做同样的事情。我想我接近,但由于某些原因,在我的浏览器控制台给我一个错误如下:


  

[错误]语法错误:不能使用关键字VAR作为变量名。
    (匿名函数)


下面是我的code为止。我已经包括(按顺序)我在jQuery的尝试,然后我工作的JavaScript code(注释掉),以及我的功能更新下拉列表。我最终会试图重现jQuery中的功能以及。谁能告诉我为什么我得到这个错误,或者我在我的jQuery做错了吗?谢谢你。

  VAR clientArray = [];
        变种clientInput =的document.getElementById(newClt);
        变种SEL =的document.getElementById(cltList);
            $(#addCltBtn)。点击(函数(){                VAR找到= $ .inArray(clientInput,clientArray)GT; -1;                如果(发现> = 0){
                    //元素被发现的errorMessage更新DIV
                    $(的errorMessage)的.html(此客户端已存在);                }其他{
                        clientArray.push(clientInput.value);                }
                });
    //
    //的document.getElementById(addCltBtn)。=的onclick功能(){
    //
    //执行console.log(clientArray.length);
    //如果(!包含(clientArray,clientInput.value)){
    // clientArray.push(clientInput.value);
    //的console.log(对象:+ clientArray.join(,));
    // updateDropList(clientInput.value);
    //}
    //否则警报(你已经有一个客户端用这个名字!);
    //}
        功能updateDropList(值){
                VAR选择=使用document.createElement('选项');
                opt.innerHTML =价值;
                opt.value =价值;
                sel.appendChild(OPT);        }函数包含(A,OBJ){
            对于(VAR I = 0; I<则为a.length;我++){
                如果(A [I] === OBJ){
                    返回true;
                }            }
            返回false;
        }


解决方案

  VAR找到= $ .inArray(clientInput,clientArray)GT; -1;

你为什么把这个说法比较?这就是你扔的错误。你是否发现大于或等于0的其它地方,所以去除> -1应该使一切工作。

I'm trying to improve on some javaScript I wrote to push elements into an array if they were not previously in the array. Then, I want that array displayed in a selection field. I had this all working in javascript but thought it would be an interesting exercise for myself to try doing the same thing in jQuery. I think I'm close but for some reason, the console in my browser gives me an error as follows:

"[Error] SyntaxError: Cannot use the keyword 'var' as a variable name. (anonymous function)"

Here is my code so far. I've included (in this order) my attempt at the jQuery and then my working javascript code (commented out), as well as my function updating the drop list. I'll eventually attempt to recreate that function in jQuery as well. Can anyone tell me why I'm getting this error and or what I'm doing wrong in my jQuery? Thank you.

     var clientArray = [];
        var clientInput = document.getElementById("newClt");
        var sel = document.getElementById("cltList");


            $("#addCltBtn").click(function(){

                var found = $.inArray(clientInput, clientArray) > -1;

                if(found >= 0) {
                    //Element was found update errorMessage div
                    $(errorMessage).html("This Client Already Exists");

                }   else{
                        clientArray.push(clientInput.value);

                }   
                });
    //  
    //      document.getElementById("addCltBtn").onclick=function(){
    //          
    //          console.log(clientArray.length);
    //          if (!contains(clientArray,clientInput.value)) {
    //              clientArray.push(clientInput.value);
    //              console.log("Objects: " + clientArray.join(", "));
    //              updateDropList(clientInput.value);
    //          }
    //          else alert("You already have a client by that name!");
    //          }


        function updateDropList(value){
                var opt = document.createElement('option');
                opt.innerHTML = value;
                opt.value = value;
                sel.appendChild(opt);

        }

function contains(a, obj){
            for(var i =0; i< a.length; i++){
                if(a[i] === obj){
                    return true;
                }

            }
            return false;
        }

解决方案

var found = $.inArray(clientInput, clientArray) > -1;

Why are you placing a comparison in this statement? This is what is throwing your error. You check if found is greater than or equal to 0 elsewhere, so removing the > -1 should make everything work.

这篇关于阵推按一下按钮的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆