2011-11-07 61 views
0

還有就是頁面上的複選框問題與jQuery類

<input 
     name='<%=htmlServiceId %>' 
     <%--class='parent-<%= service.ParentId.Value.GetCustomerServiceId() %>'--%> 
     class='parent-<%=service.ParentId.Value.GetCustomerCategoryId() %> x-form-checkbox x-form-field' 
     type='checkbox' 
     id="<%=htmlServiceId%>" 
    /> 

下面是結果HTML的外觀

<input name="s8" class="parent-s18" id="s8" type="checkbox"/> 

時checkBos有一類(parent-sXX)我覺得這個複選框前(和其他像這樣)通過這種方式

//........................ 
var temp = $(this).attr('class').split('-'); 
    var parentId = temp[1]; 
    if ($(this).attr('checked')) { 
       $('#' + parentId).attr('checked', 'checked'); 
       //....................... 
} 

}

但是現在checkBox有幾個類(parent-sXX x-form-checkbox x-form-field),並且通過按類查找checkBox的方法無效。

如何解決?

更新:除了使用data屬性之外,還有什麼方法嗎?

+1

斯普利特類輸出(母體隨心的X形複選框的X表單域)通過「空格」字 - 然後刪除所有誰dosent包含「父 - 」 - 並使用您的正常代碼? :) –

+0

我不知道該怎麼做。請給我一個。 – Alexandre

+0

爲什麼你不想使用數據方法? –

回答

2

如果你想維護你的原代碼,你可以過濾掉這個類。

參見:http://jsfiddle.net/mSRZY/

$(':checkbox').change(function() { 

    //get classes 
    var getClass = $(this).attr('class'); 
    //seperate them into an array 
    var classes = getClass.split(' '); 

    //loop the array, replace getClass if it find the "search param" 
    $.each(classes, function(index,value) { 
     if(value.indexOf("parent") != -1) { 
      getClass = value; 
     } 
    }); 

    alert(getClass); 
}); 
+0

你應該proberbly將其插入一個新的變種,而不是的getClass,因此,如果發現或沒有,你可以檢查:) –

0

使用數據屬性和jQuery的data方法:

HTML

<input 
    name='<%=htmlServiceId %>' 
    class='x-form-checkbox x-form-field' 
    type='checkbox' 
    id="<%=htmlServiceId%>" 
    data-parentId="<%=service.ParentId.Value.GetCustomerCategoryId() %>" 
/> 

JS

var parentId = $(this).data('parentId'); 
if ($(this).attr('checked')) { 
    $('#' + parentId).attr('checked', 'checked'); 
} 

不使用數據屬性,你可以這樣做:

var temp = $(this).attr('class').split(' '), parentId; 
for (var i = 0; i < temp.length; i++) { 
    if (temp[i].indexOf('parent-') === 0) { 
     parentId = temp[i].split('-')[1]; 
    } 
} 
if ($(this).attr('checked')) { 
    $('#' + parentId).attr('checked', 'checked'); 
} 

JSFiddle Example

1

我認爲這將是容易,只需添加另一個data-屬性和使用,而不是試圖從其中一個類提取ID ..

<input 
     name='<%=htmlServiceId %>' 
     <%--class='parent-<%= service.ParentId.Value.GetCustomerServiceId() %>'--%> 
     class='parent-<%=service.ParentId.Value.GetCustomerCategoryId() %> x-form-checkbox x-form-field' 
     type='checkbox' 
     data-parentid="<%= service.ParentId.Value.GetCustomerServiceId() %>" 
     id="<%=htmlServiceId%>" 
    /> 

,並使用

var parentId = $(this).data('parentid'); 
if ($(this).attr('checked')) { 
       $('#' + parentId).attr('checked', 'checked'); 

更新

既然你不能使用data這裏是分裂類的方式..

var itemClasses = $(this).attr('class').split(' '); 
var parentId = ''; 

for (var i = 0; i < itemClasses.length; i++) { 
    if (itemClasses[i].indexOf('parent-s') == 0) { 
     parentId = itemClasses[i].split('-')[1]; 
     break; 
    } 
} 

if ($(this).attr('checked')) { 
    $('#' + parentId).attr('checked', 'checked'); 
} 
+0

這不是我所需要的。 – Alexandre

+0

@alex,爲什麼不呢? –

+0

@alex,更新,回答一個辦法找到類和拆分它,你最初做的.. –

1

如果你想保持你的HTML原樣,這應該得到你從類列表中選擇所需的數量 -

var parentId = $(this).attr('class').match(/parent-s(\d+)/)[1]; 
    if ($(this).attr('checked')) { 
       $('#' + parentId).attr('checked', 'checked'); 
       //....................... 
}