2014-04-29 210 views
1

我有一個foreach循環,我有一個單選按鈕,如下所述。動態生成單選按鈕名稱?

我下面的代碼:

@section Scripts { 

    <script type="text/javascript" src="@Url.Content("~/Scripts/ViewModel/LeadQualification.js")"></script> 
} 


    <div style="width: 1100px; height: 700px;" class="container"> 
     <div style="width: 1100px; height: 110px; background-color: burlywood"> 
      <table> 
       <tr> 
        <td style="width: 400px; height: 110px; background-color: burlywood;" align="left"> 
         <h3>Qualification Factor</h3> 
        </td> 
        <td style="width: 400px; height: 110px; background-color: burlywood" align="center"> 
         <h3>Score</h3> 
        </td> 
        <td style="width: 400px; height: 110px; background-color: burlywood" align="center"> 
         <h3>Comment</h3> 
        </td> 
       </tr> 
      </table> 
     </div> 
     <div style="width: 1100px; height: 580px; border: 1px solid; overflow-x: scroll; overflow-y: scroll;" >  
          <table> 
           <tr> 
            <td style="width: 200px; height: 50px;" align="center"></td> 
            <td style="width: 200px; height: 50px;" align="center"> 
             <h4>Negative</h4> 
            </td> 
            <td style="width: 200px; height: 50px;" align="center"> 
             <h4>Neutral</h4> 
            </td> 
            <td style="width: 200px; height: 50px;" align="center"> 
             <h4>Positive</h4> 
            </td> 

           </tr> 
          </table> 
      <table>    
       <tbody data-bind="foreach: leadqualificlist">      
        <tr> 

         <td style="width: 250px; height: 130px;"> 

          <h4 data-bind="text: $data.LeadQualifition"></h4>      
         </td> 
         <td style="width: 580px; height: 80px;"> 

          <table> 
           <tr> 
            <td style="width: 210px; height: 50px;" align="center"> 

             <h4 data-bind="text: $data.Negativescore" 0-1-2-3> 
              <br /> 
              </h4> 
            </td> 
            <td style="width: 210px; height: 50px;" align="center"> 
             <h4 data-bind="text: $data.Neutralscore" 4-5-6> 
              <br /> 
              </h4> 
            </td> 
            <td style="width: 210px; height: 50px;" align="center"> 
             <h4 data-bind="text: $data.Positivescore" 7-8-9> 
              <br /> 

              </h4> 
            </td> 
           </tr> 


           <tr> 
            <td style="width: 210px; height: 50px;" align="center"> 


             <input type="radio" style="width: 20px; height: 20px;" name="grp" value="0" data-bind="checked:Scoreschk" /> 
             <input type="radio" style="width: 20px; height: 20px;" name="grp" value="1" data-bind="checked:Scoreschk" /> 
             <input type="radio" style="width: 20px; height: 20px;" name="grp" value="2" data-bind="checked:Scoreschk" /> 
             <input type="radio" style="width: 20px; height: 20px;" name="grp" value="3" data-bind="checked:Scoreschk" /> 
            </td> 
            <td style="width: 210px; height: 50px;" align="center"> 
             <input type="radio" style="width: 20px; height: 20px;" name="grp" value="4" data-bind="checked:Scoreschk" /> 
             <input type="radio" style="width: 20px; height: 20px;" name="grp" value="5" data-bind="checked:Scoreschk" /> 
             <input type="radio" style="width: 20px; height: 20px;" name="grp" value="6" data-bind="checked:Scoreschk" /> 

            </td> 
            <td style="width: 210px; height: 50px;" align="center"> 
             <input type="radio" style="width: 20px; height: 20px;" name="grp" value="7" data-bind="checked:Scoreschk" /> 
             <input type="radio" style="width: 20px; height: 20px;" name="grp" value="8" data-bind="checked:Scoreschk" /> 
             <input type="radio" style="width: 20px; height: 20px;" name="grp" value="9" data-bind="checked:Scoreschk" /> 

            </td> 

           </tr> 


          </table> 
         </td> 
         <td> 
          <textarea style="width: 300px; height: 110px;" data-bind="value: $data.Commenttext">Text Box 
          </textarea> 

         </td> 
        </tr>         
       </tbody> 




      </table> 

      <table style="width: 100%"> 
       <tr> 
        <td></td> 
        <td></td> 
        <td align="center"> 

         @*<button data-bind="click :$root.create" style="background-color:burlywood">Save</button>*@ 
         <input type="submit" title="Save" value="Submit" data-bind="click:$root.create" style="background-color: burlywood" /> 
         <input type="submit" title="Save" value="Next Stage" style="background-color: burlywood" /> 
        </td> 
       </tr> 

      </table> 
     </div> 
    </div> 

問題在這裏我想有一個單選按鈕分組基於一行。如果我有一個靜態名稱,即名稱=「grp」,當我selet其他單選按鈕的其他選擇正在實現。

所以我想分配一個動態名稱?在這種情況下,有沒有更好的替代方案適合我。

你的努力是值得讚賞 問候

+0

你的其他選擇的意思越來越影響? – lampdev

+0

在我上面的代碼中使用複選框 –

+0

有一個foreach循環,它將運行10次。根據我的代碼,我會得到10 tr的每個TR有一些單選按鈕應該被分組。我面對的問題很簡單,因爲我有我的所有TR的單選按鈕的名稱相同,當我選擇其他正在生效 – user3483538

回答

1

就嘗試像下面將工作。

data-bind='attr: { name: "grp" + $data.Negativescore}' 

請嘗試以下方法:

<input type="radio" style="width: 20px; height: 20px;" value="0" data-bind="checked:Scoreschk, attr: { name: "grp" + $data.Negativescore}" /> 
+0

這並不能回答這個問題。 – Swati

+0

這是對用戶評論的回覆:添加了我的整個html代碼,你可以找到data-bind =「text:$ dat a.Negativescore「在我的foreach循環中,每次循環運行時,給定的數據綁定都將動態地靜態化。我只需要將上面的數據綁定代碼綁定到我的TR中的單選按鈕中。我試着像name =「grp」+ $ data.Negativescore //不工作任何想法 –

+0

它們不能是兩個數據綁定 – user3483538

1

看起來你想要一個td下radion按鈕應該屬於同一組。
要實現它,您可以將id分配給td s並將其附加到單選按鈕名稱。因此,一個td下的所有無線電都屬於同一組,並且不會影響其他選擇。
Fiddle

+0

是隻是檢查小提琴。多數民衆贊成什麼我需要,但你給名稱靜態,我需要動態 – user3483538

+0

@ user3483538添加您的腳本動態填充收音機。 – xyz

+0

好的腳本將會很大。使簡單我會得到一個在foreach循環中的數據列表。 list [0]包含4個listitems,可能我可能有list [n]。我可以使用列表[0]中的任何一個值使我的TR名稱變爲動態。 – user3483538

0

動態追加單選按鈕?

使用jQuery的...

  1. 得到單選按鈕

    var rbtn = $(':radio[name=" your radio btn name here "]:checked').val(); 
    
  2. 添加單選按鈕

    $('# your div id here').append('<input type="radio" style=.....>' button!! '</input>'); 
    
  3. 動態數據 - > AJAX使用

    通過JSON對象BTN
  4. 附加無線電

    success : function(obj) { 
        $('# your div id here').append('<input type="radio" style=.....>'+obj.(your code here)+'</input>'); 
    } 
    
+0

你究竟想要回答什麼?我不能看到任何像AJAX,JSON – xyz

0

該線路應該是在表塔格以外

<%String day="grp" %> 

這條線可以是TBODY標籤後

<% 
    for(int i=0;i<3;i++) 

    %> 

在你的td tage中使用<td id=day<%=i> >

所以你的td ID將是grp0,grp1,grp2等等。

+0

hie,我需要一個相同的名稱,以整個9單選按鈕在TR ..但每個tr應該有所不同。 – user3483538

+0

<%for(int j = 0; i <3;j++)%> and give tr id name – JohnRose

+0

dude我有foreach循環通過knockout。我想我不能使用上面給出的東西。我的循環代碼看起來像**數據綁定=「foreach:leadqualificlist」**。這個我必須使名稱動態 – user3483538

0

你可以給值和名稱相同的值..

名= 「0」 值= 「0」

<td style="width: 210px; height: 50px;" align="center"> 
       <input type="radio" style="width: 20px; height: 20px;" name="0" value="0" data-bind="checked:Scoreschk" /> 
       <input type="radio" style="width: 20px; height: 20px;" name="1" value="1" data-bind="checked:Scoreschk" /> 
       <input type="radio" style="width: 20px; height: 20px;" name="2" value="2" data-bind="checked:Scoreschk" /> 
       <input type="radio" style="width: 20px; height: 20px;" name="3" value="3" data-bind="checked:Scoreschk" /> 
      </td> 
+1

然後所有的單選按鈕將屬於不同的組,並且您可以選擇每個單選按鈕 – xyz

+0

我正在尋找不同的隊友:rolind – user3483538

+0

那麼你可以使用一些服務器端腳本,比如php,jsp,然後你就會得到你想要的東西,謝謝 – rolindroy