2012-03-29 16 views
1

我有一個名爲CustomPickedTableTable,這個Table具有屬性的行,例如<td Data-question-id="5">Example</td>,有些行根本沒有任何屬性。只是<td>example</td>.如果ID缺失用jquery填充另一個隱藏字段中的行

我想要做的是能EM分類到不同的hiddenfields,這些都是我的hiddenfields:

@Html.HiddenFor(model => model.SelectedCustomQuestions, new { @id = "SelectedQuestionsWithAttr" }) 
@Html.HiddenFor(model => model.SelectedQuestions, new { @id = "SelectedQuestionsWithNoAttr" }) 

,我現在所擁有的代碼是與屬性"data-question-id"所有行被填充SelectedQuestionsWithAttr那是我擁有屬性的行的隱藏字段。

但我想我的jquery代碼也填充沒有屬性的行被填充到我的SelectedQuestiosnWithNoAttr hiddenfield。

這是剛剛填補SelectedQuestionsWithAttr hiddenfield代碼:

   var selectedQuestionsWithAttr = $("#SelectedQuestionsWithAttr"); 
       var currentIds = new Array(); 

       $("#CustomPickedTable").find("td").each(function() { 
        var clickedId = $(this).attr("data-question-id"); 
        currentIds.push(clickedId); 
       }); 

       selectedQuestionsWithAttr.val(currentIds.join(",")); 

       $("form").submit(); 
      } 

有沒有我可以添加到我的jQuery代碼這方面的任何解決方案?

由於提前

回答

0

您需要添加東西<td>標籤可以識別它們:

<td id="[email protected](Model.SelectedQuestions.IndexOf(variable))"> 

然後jQuery的是:

var $qWithAttr = $("#SelectedQuestionsWithAttr"); 
var $qWithoutAttr = $("#SelectedQuestionsWithNoAttr"); 
var currentIds = new Array(); 
var missingIds = new Array(); 
$("#CustomPickedTable td[data-question-id]").each(function() { 
    currentIds.push($(this).attr("data-question-id")); 
}); 
$("#CustomPickedTable td:not([data-question-id])").each(function() { 
    missingIds.push($(this).attr("id")); 
}); 
$qWithAttr.val(currentIds.join(",")); 
$qWithoutAttr.val(missingIds.join(",")); 

$("form").submit(); 
+0

它應該可以只是「如果表td沒有任何屬性做這個」 – Obsivus 2012-03-29 10:47:57

+0

這就是td:not([data-question-id])選擇器是。你在那個函數中做的是由你決定的,但你確實要求將這些行添加到你的SelectedQuestionsWithNoAttr變量中,爲此,它們需要以某種方式被識別。 – CodingIntrigue 2012-03-29 10:58:11

相關問題