admin管理员组

文章数量:1323714

Hi I have multiple elements with the same structure in my application. Second div element's id varies as per the ment id in the db which is unique. There are elements with the id 'vote_up' and 'vote_down'. This gets repeated for each ment.What happens is that, as I mentioned, there are multiple ments. I want to perform an Ajax request. First of this structure functions properly using ajax, but the rest does an http request. Btw I am developing a rails application and I am using jQuery.

<div id="post_ment">
john<i> says </i> Comment<br/>

<div id="ment_10_div">
**<form action="/ments/vote_up" id="vote_up" method="post">**
         <div style="margin:0;padding:0;display:inline">
               <input name="authenticity_token" type="hidden" 
               value="w873BgYHLxQmadUalzMRUC+1ql4AtP3U7f78dT8x9ho=" />
         </div>
     <input id="Comment_place_id" name="Comment[post_id]" type="hidden" value="3" />
     <input id="Comment_id" name="Comment[id]" type="hidden" value="10" />
     <input id="Comment_user_id" name="Comment[user_id]" type="hidden" value="2" />
     <input name="mit" type="submit" value="Vote up" />
</form>

<label id="ment_10">10</label>

**<form action="/ments/vote_down" id="vote_down" method="post">**
        <div style="margin:0;padding:0;display:inline">
            <input name="authenticity_token" type="hidden" 
            value="w873BgYHLxQmadUalzMRUC+1ql4AtP3U7f78dT8x9ho=" />
        </div>
    <input id="Comment_place_id" name="Comment[place_id]" type="hidden" value="3" />

    <input id="Comment_id" name="Comment[id]" type="hidden" value="10" />
    <input id="Comment_user_id" name="Comment[user_id]" type="hidden" value="2" />
    <input name="mit" type="submit" value="Vote Down" />
</form>
</div>      

Can you please help me to solve this Thanks

Hi I have multiple elements with the same structure in my application. Second div element's id varies as per the ment id in the db which is unique. There are elements with the id 'vote_up' and 'vote_down'. This gets repeated for each ment.What happens is that, as I mentioned, there are multiple ments. I want to perform an Ajax request. First of this structure functions properly using ajax, but the rest does an http request. Btw I am developing a rails application and I am using jQuery.

<div id="post_ment">
john<i> says </i> Comment<br/>

<div id="ment_10_div">
**<form action="/ments/vote_up" id="vote_up" method="post">**
         <div style="margin:0;padding:0;display:inline">
               <input name="authenticity_token" type="hidden" 
               value="w873BgYHLxQmadUalzMRUC+1ql4AtP3U7f78dT8x9ho=" />
         </div>
     <input id="Comment_place_id" name="Comment[post_id]" type="hidden" value="3" />
     <input id="Comment_id" name="Comment[id]" type="hidden" value="10" />
     <input id="Comment_user_id" name="Comment[user_id]" type="hidden" value="2" />
     <input name="mit" type="submit" value="Vote up" />
</form>

<label id="ment_10">10</label>

**<form action="/ments/vote_down" id="vote_down" method="post">**
        <div style="margin:0;padding:0;display:inline">
            <input name="authenticity_token" type="hidden" 
            value="w873BgYHLxQmadUalzMRUC+1ql4AtP3U7f78dT8x9ho=" />
        </div>
    <input id="Comment_place_id" name="Comment[place_id]" type="hidden" value="3" />

    <input id="Comment_id" name="Comment[id]" type="hidden" value="10" />
    <input id="Comment_user_id" name="Comment[user_id]" type="hidden" value="2" />
    <input name="mit" type="submit" value="Vote Down" />
</form>
</div>      

Can you please help me to solve this Thanks

Share Improve this question asked Jun 3, 2010 at 20:38 felixfelix 11.6k13 gold badges70 silver badges95 bronze badges 2
  • 11 Using the same ID twice in the same document is not valid HTML. Fix your HTML. – Matti Virkkunen Commented Jun 3, 2010 at 20:41
  • 3 You could use the same class for the elements with same behavior instead of same ID. As already mentioned, IDs are supposed to be unique :] – Ju Nogueira Commented Jun 3, 2010 at 21:25
Add a ment  | 

2 Answers 2

Reset to default 4

Yes, as Matti mentioned above, by the W3C standards ID has to be unique. A nice workaround would be to postfix ment's db ID, e.g. <input id="Comment_39127438"...

Expanding upon what's already been said, the way you should implement this is:

<div id="ment_10" class="ment">
</div>

Then you can select all ments with:

$('.ment')

or a single ment with:

$('#ment_10")

本文标签: javascriptDOM Elements with same id and jQueryStack Overflow