2016-11-08 8 views
0

HTMLコード:要素クラスからデータ値を取得する方法は?

<input type="file" name="efile" id="efile" data-cat="electronics" style="width:100%" class="alertme"> 
<input type="file" name="dfile" id="dfile" data-cat="Decoratives" style="width:100%" class="alertme"> 
<input type="file" name="kfile" id="kfile" data-cat="kitchen" style="width:100%" class=""> 
<input type="file" name="hdfile" id="hdfile" data-cat="Home" style="width:100%" class=""> 
<input type="file" name="ffile" id="ffile" style="width:100%" data-cat="Furnitures" class="alertme"> 

どのように私はクラスAlertMeしが存在しているデータ・猫の属性値を得ることができますか?私の質問を読んでくれてありがとう。私は英語が弱いので、文法やスペルミスをしてしまったら、私にお詫び申し上げます。ここ

+0

あなたはデータのクリックイベントまたはデフォルトの実行ページを取得するとき? –

答えて

0

あなたは、データの属性やループを取得するには、この

$(document).ready(function(){ 
 
    $(".alertme").each(function(){ 
 
var val = $(this).data('cat'); 
 
    alert(val); 
 
}); 
 
})
<input type="file" name="efile" id="efile" data-cat="electronics" style="width:100%" class="alertme"> 
 
<input type="file" name="dfile" id="dfile" data-cat="Decoratives" style="width:100%" class="alertme"> 
 
<input type="file" name="kfile" id="kfile" data-cat="kitchen" style="width:100%" class=""> 
 
<input type="file" name="hdfile" id="hdfile" data-cat="Home" style="width:100%" class=""> 
 
<input type="file" name="ffile" id="ffile" style="width:100%" data-cat="Furnitures" class="alertme"> 
 
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

+0

この機能を変更して実行するにはどうすればよいですか? –

+0

あなたはちょうど '$( 'input [type = file]')を使わなければなりません。変更(function(){ //あなたのコード }); ' – Bharat

2

はこれを達成するためのコードです:

$(".alertme").each(function(){ 
    // Here you can fill your array or whatever you want.. 
    $(this).attr("data-cat"); 
}); 
+0

この機能を変更するにはどうしたらいいですか? –

+0

http://stackoverflow.com/questions/2721250/jquery-change-method-on-input-type-file入力タイプファイルのこの変更機能を参照してください。 –

0

使用attr()のように試みることができる値をプッシュしますデータ配列へ

var aDataValues = []; 
for(var i = 0; i < aAlertMe.length; i++){ 
    aDataValues.push(aAlertMe[i].dataset.cat); 
} 

:配列の各要素は、別の配列にそのデータを挿入するために

var aAlertMe = window.document.getElementsByClassName("alertme"); 

第二に、:

var data = []; 
 
$(".alertme").change(function() { 
 
    $(".alertme").each(function() { 
 
    data.push($(this).attr("data-cat")); 
 
    }); 
 
    console.log(data); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="file" name="efile" id="efile" data-cat="electronics" style="width:100%" class="alertme"> 
 
<input type="file" name="dfile" id="dfile" data-cat="Decoratives" style="width:100%" class="alertme"> 
 
<input type="file" name="kfile" id="kfile" data-cat="kitchen" style="width:100%" class=""> 
 
<input type="file" name="hdfile" id="hdfile" data-cat="Home" style="width:100%" class=""> 
 
<input type="file" name="ffile" id="ffile" style="width:100%" data-cat="Furnitures" class="alertme">

+0

この関数を変更するには? –

+0

@ShahRushabhは変更イベントでコードをラップします。私の更新された回答を参照してください – madalinivascu

0

まずあなたは、そのクラスを持つすべての要素を取得する必要がありますそして、あなたはaDataValuesの中にすべての値を持っています。

+0

この関数を変更するにはどうしたらいいですか? @ShahRushabh Easy。 –

+0

{ /*ここにコードがあります*/ } ' – xale94

0

これを試してみてください。

$("input[type='file']").each(function() { 
    if($(this).hasClass("alertme")) 
     alert($(this).data("cat")); 
}); 
+0

この関数を変更するにはどうすればいいですか? –

関連する問題