2014-04-29 162 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