2012-05-29 137 views
1

我有一个表格,其中几列包含选择元素。在tfoot中,我有一个输入元素,用于根据select元素中的选定值对行进行过滤。选择元素更改后选择属性未更改

当加载该表并直接过滤该列与选择,比它的工作原理和表进行过滤。

但要做出的选择要素的变化时,滤波器功能不考虑该值已更改的任何通知。

检查出来的HTML,我可以看到“选择”属性仍然在加载时所选择的选项。因此,在进行实际更改时(在FF和Chrome中均标识),它不会更新。

但是从jQuery的,搜索选定值(.find(":selected"))的作品。所以我想我可以使用它来查找值,然后将所选属性设置为所选内容。这就是我试图去做的:

$("select[id^='qu_owner']").live('change', function (e) { 
    var owner = $(this).val(); 
    console.log(owner); 
    var ticketid = $(this).attr('id').substr(9); 
    $($(this) + " option[value=" + owner + "]").attr("selected", "selected"); //Update: this has been removed 
}); 

但是选中的属性仍然没有在元素中更新。

任何线索如何做到这一点?我需要过滤器才能工作,它正在查看所选的attibute。是不可能对select元素进行这种更新?


UPDATE

好吧,基于以下我的评论和答案的理解有在做什么,我做了上面的更好的方法。因此,而不是使用$(this).find(“:selected”)。val();我现在使用$(this).val();.此外,我确实删除了最后一行,因为我知道不应该尝试设置选定的属性。

此外,我现在明白,我有实际问题的代码是过滤器功能。这是数据表所以这是一个插件,但是这是显著部分:

ADATA [i]是实际的表格单元格。因此,它只是文本,但在这种情况下,它是我选择元素的文本。我认为以下是朝着正确的方向前进,但仍然没有工作(检查旁边CONSOLE.LOG-行注释):

function(oSettings, aData, iDataIndex) { 

       var ret = true;  

        //Loop through all input fields i tfoot that has class 'sl_filter' attached            
        $('tfoot .sl_filter').each(function(i, obj){ 

         //$(this) can be used. Get the index of this colum. 
         var i = $("tfoot input").index($(this));       

         //Create regexp to math 
         var r = new RegExp($(this).val(), "i"); 

         //Get the selected option from the column 
         console.log($(aData[i]).attr('id')); //Properly prints the ID 
         console.log($(aData[i] + " option:selected").text()); //Prints all options, not only the selected on 
         console.log($(aData[i] + " option:selected").val()); //Prints the value of the id that was selected when data was loaded, even if a new option has been chosen 
         console.log($(aData[i]).val()); //Prints the value of the id that was selected when data was loaded, even if a new option has been chosen 

         var str = $(aData[i] + " option:selected").text();       

         /*Test to see if there is a match or if the input value is the default 
         (the initial value of input before it has any fokus/text) */       
         if(r.test(str) || $(this).val()=="Search"){ 
          //Return true only exits this function 
          return true; 
         }else{ 

          /*Return false returns both function an .each. Retain 'false' in a variable scoped 
          to be reached outside the .each */         
          ret = false; 
          return false; 
         } 
        }); 
        //Return true or false 
        return ret;       
       } 

这是所选元素我需要弄个文本。这就是应该过滤的内容。我怎样才能做到这一点?


更新

为了缩小范围,我已创建了什么是必要的一个的jsfiddle。这是所有关于如何提取从所选选项的文本:jsfiddle

ADATA [I] =表格单元格。在jsfiddle中,我使用“sel”作为单元格内容的变量。 sel的文本从aData [i]复制而来。

+1

因此,您倾听选择“更改”,然后选择当前选择的选项?!?! – ManseUK

+0

@ManseUK显然他试图愚弄其他一些jQuery函数误以为_attribute_已经改变,而不是_property_。 – Alnitak

+0

@Alnitak听起来理智....也许是解决这个问题可能是一个好主意.... – ManseUK

回答

1

没有必要更改<option>属性 - 这应该包含元素的原始状态,因为它是从服务器下载的。

你的过滤器应检查selected财产所选择的选项,如:

.filter(function() { 
    return this.selected; // check the boolean property 
}) 

这也是:selected伪选择如何工作 - 它会检查财产的电流值,而不是属性

请注意,在change处理程序的<select>元素中,您可以使用this.value来获取当前选定的值。

$('select').on('change', function() { 
    // current value 
    var value = this.value; 

    // text of the currently selected option. 
    var text = this.options[this.selectedIndex].text; 
} 
+0

''''''''''''''就像'prop'一样工作。 – gdoron

+0

@gdoron是的,我知道,但他实际上没有显示那些还没有工作的代码。他展示的所有东西都是一种懒散的工作。 – Alnitak

+0

我在他的代码中纠正的所有事情都不是不工作的代码? – gdoron