2016-05-12 4 views
1

JQueryで属性値を取得しようとしていますが、jquery.minというエラーが発生しています。 js:2 Uncaught TypeError:PRODで 'length'を検索するために 'in'演算子を使用できませんJQueryで属性値を取得する(Uncaught TypeError: 'in'演算子を使用して 'length'を検索できません)

ダウングレードJQueryバージョン1.10.xxに関するいくつかのアドバイスを見ましたが、すべてのバージョンのJQuery 。

アドバイスをお願いしますか?事前に

おかげ..

私のコードは以下の通りです:

<html lang=""> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title></title> 
</head> 

<body> 
    <input test="PROD" value="2" type="text"> 
    <input test="DEV" value="3" type="text"> 
    <input type="button" value="Write It" onclick="writeIt()"/> 
</body> 
</html> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
<script> 
function writeIt() { 
    $.each($('input').attr('test'), function() { 
     var testdata = $(this).attr('test'); 
     //$(this).val(testdata); 
     console.log(testdata); 
    }); 

} 
</script> 

答えて

1

あなたは試すことができます

<script> 
function writeIt() { 
    $('input:not([type="button"])').each(function() { 
     var testdata = $(this).attr('test'); 
     //$(this).val(testdata); 
     console.log(testdata); 
    }); 

} 
</script> 

デモ

function writeIt() { 
 
    $('input:not([type="button"])').each(function() { 
 
     var testdata = $(this).attr('test'); 
 
     //$(this).val(testdata); 
 
     console.log(testdata); 
 
    }); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input test="PROD" value="2" type="text"> 
 
<input test="DEV" value="3" type="text"> 
 
<input type="button" value="Write It" onclick="writeIt()"/>

+1

ありがとうございました。あなたの答えの後、自分のコードを 'code' $(" input [test] ")に変更しました。 (this)).val(testdata); console.log(testdata); }); 'code'を実行すると正しく動作します。私のコードで間違ったものが何であるか教えてください。 – Grcn

+1

@Grcn間違った方法で、$( 'input')。attr( 'test')..を使ってループし、attr()を取得するオブジェクトが必要です。$ .each ($( "input [test]")))$( "input [test]")セレクタもとても良いです..あなたには良い運があります:) –

+2

非常にありがとうございました:) @Mohamed – Grcn

関連する問題