2011-01-27 142 views
0

获取ID,我有以下形式从serializeArray

<form action="test.php" id="loginform" name="loginform" method="post"> 
    <input name="title[]" id="title1" type="text" value="" tabindex="1" /> 
    <input name="title[]" id="title2" type="text" value="" tabindex="2" /> 
    <input name="title[]" id="title3" type="text" value="" tabindex="3" /> 
    <input type="submit" name="submit" value="Submit" id="submit" tabindex="4" /> 
</form> 

我能够获得元素的名称,但使用此代码不是它的ID。

$('#loginform').bind('submit', function() { 
    var elements = $(this).serializeArray(); 
    $.each(elements, function(i, element) { 
     var temp = $('#' + element['name']); 
     var name = this.name; alert(name); 
     var id = this.id; alert(id); ///even id = this.attr("id"); not getting 

     var value = this.value; 
     (temp.val() == '') ? temp.css({'background': '#FFC4C4', 'border': '1px solid #F00'}) : temp.removeClass('hightlight'); 
    }); 
    return false; 
}); 

Demo

回答

1

更新在此为你解答:

此代码:

var id = $(this).id;alert(id); 

应该成为这样的:

var id = $('input[name="' + name + '"]').attr("id"); 
alert(id); 

看到代码干活g here:http://jsfiddle.net/Ct8zf/5/

请注意,根据.serializeArray()文档,唯一被序列化的元素是名称和值。

希望这会有所帮助。

+0

尝试了所有的可能性,但它说undefined var id = this.id; alert(id); var id = this.attr(“id”); alert(id); – 2011-01-27 09:42:34