2012-05-04 13 views
0

私はクリックされたイメージのIDをつかんで、フォームでそれを送信しようとしています(隠されたimagenr入力の値を設定することによって)。しかし、私はIDを引き出す方法を知らない。イメージIDを選択

$(document).ready(function() { 
      $(".test-img").click(function() { 

       var imagevar = // How to grab the clicked img id? 
       $("#imagenr").val(imagevar); // set the img id to the hidden input 
       $("form#test-form").submit(); // submit the form 

      }) 
     }) 




<form action="#" method="post" id="test-form"> 
<input type="hidden" name="imagenr" id="imagenr" value=""> 

<img class="test-img" id="1" src="images/image1.jpg" alt=""/> 
<img class="test-img" id="2" src="images/image2.jpg" alt=""/> 

</form> 

答えて

3

だけで、jQueryの中でそれをラップする必要がthis.idを使用していません。

$(this).attr('id'); 

幸運:

var imagevar = this.id; 

Example on jsfiddle

0
$(".test-img").click(function() { 
    var imagevar = $(this).attr('id'); 
    $("#imagenr").val(imagevar); // set the img id to the hidden input 
    $("form#test-form").submit(); // submit the form 
}) 
0
$(document).ready(function() { 
     $(".test-img").click(function() { 

      var imagevar = $(this).attr('id'); 
      $("#imagenr").val(imagevar); // set the img id to the hidden input 
      $("form#test-form").submit(); // submit the form 

     }) 
    }); 
0

あなたは次のようなものを使用して、直接、任意の属性の値を取得することができます。

関連する問題