English 中文(简体)
查找最接近对象类 - Jquery
原标题:Find the closest class of object - Jquery

< 强度 > Jquery

  var courseSelect =  $(".courseSelected");
    $(".courseSelected").click(function()
    {
        if($(this).attr( checked ))
        {
            var selects = $(this).parent().next(".select").attr( title );
            alert(selects);
        }
    });

< 强 > HTML

         <div>
            <div style="float:left;">
                <input name="course[]" type="checkbox" value="AUTOCAD" class="courseSelected" />
            </div>

            <div style="width:100px; float:left;">AUTOCAD</div>
            <select name="fromto[]" class="select small" disabled="disabled" title="Batch">
                <option value="">--Select--</option>
                <option value="May 2012,June 2012">Autocad 1(11:30 AM)</option>
                <option>Batch Name</option>
            </select>
        </div>

通过选择要找到选中元素的复选框, 我尝试过与最亲近的元素和父元素(. next ()) 一起尝试过, 但在这种情况下似乎行不通 。

最佳回答
...
if($(this).attr( checked ))  {
    var selects = $(this).parent().parent().find(".select").attr( title );
    alert(selects);
}
...

$(this)          /* checkbox */
.parent()        /* div parent */
.parent()        /* div granfather */
.find(".select") /* look for .select */
.attr( title );  /* get title */

总之,为了简单起见,如果你能控制标记......我会把你的标记换成这样

<input name="course[]" type="checkbox" ... data-related-select="yourselect" />
...
<select id="yourselect">
...

所以js代码只是

var selects = document.getElementById($(this).data( related-select ));
问题回答
 $(".courseSelected").click(function(){
        if(this.checked){
            var selects = $(this)
                                 .parent() // go to checkbox parent
                                 .next() // jump to div have AUTOCAD
                                 .next(".select") // to select box
                                 .attr( title ); // retrieve the title
            alert(selects);
        }
 });

查找最接近对象头一类 - Jquery

$(this).parent().parent().find( .show-all-result ).first().show();




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签