如何在淘汰赛中绑定单选按钮的名称属性
How to bind name attribute of a radio button in knockout
我的 ASP.Net 项目中有这段代码,它使用 knockout.js:
<div>
<input type="radio" name="hearingAidConditionGroup" value="Yes" data-bind="checked: condition"> <label class="YesNo"> Yes </label>
<input type="radio" name="hearingAidConditionGroup" value="No" data-bind="checked: condition"> <label class="YesNo"> No </label>
</div>
存储在一个名为 a.cshtml
的文件中,我有另一个 html 文件,其中包含如下文件:
<div data-bind="with: testConditions()[0]">
<h4>Test conditions</h4>
@Html.Partial("~/a.cshtml")
</div>
<div data-bind="with: testConditions()[1]">
<h4>Second test conditions</h4>
@Html.Partial("~/a.cshtml")
</div>
在我的 js 文件中我有:
this.testConditions = ko.observableArray([
{ condition: "Yes" },
{ condition: "Yes" }
]);
所以问题是我如何区分单选按钮,就像上面的代码一样,我表单中的所有单选按钮都具有相同的名称,并且基本上在 div 中选择其中一个,包括 testConditions()[0]
影响 div 中的其他人,包括 testConditions()[1]
,反之亦然。
我想在数组中添加第二个属性,例如:
this.testConditions = ko.observableArray([
{ condition: "Yes", itemname: "group1" },
{ condition: "Yes", itemname: "group2" }
]);
并在 html 中使用它,例如:
<div>
<input type="radio" value="Yes" data-bind="checked: condition, name: itemname"> <label class="YesNo"> Yes </label>
<input type="radio" value="No" data-bind="checked: condition, name: itemname"> <label class="YesNo"> No </label>
</div>
然而,它没有用。有什么想法吗?
尝试这样分组
<input type="radio" name="radiobutton[0]" value="a"><br>
<input type="radio" name="radiobutton[0]" value="b"><br>
<input type="radio" name="radiobutton[0]" value="c"><br>
<br>
<input type="radio" name="radiobutton[1]" value="x"><br>
<input type="radio" name="radiobutton[1]" value="y"><br>
<input type="radio" name="radiobutton[1]" value="z">
提交表单时
单选按钮[0] = "a";
单选按钮[1] = "x";
或
尝试
jsfiddle
我找到了答案:
<div>
<input type="radio" value="Yes" data-bind="checked: condition, attr: { 'name': itemname}"> <label class="YesNo"> Yes </label>
<input type="radio" value="No" data-bind="checked: condition, attr: { 'name': itemname}"> <label class="YesNo"> No </label>
</div>
我的 ASP.Net 项目中有这段代码,它使用 knockout.js:
<div>
<input type="radio" name="hearingAidConditionGroup" value="Yes" data-bind="checked: condition"> <label class="YesNo"> Yes </label>
<input type="radio" name="hearingAidConditionGroup" value="No" data-bind="checked: condition"> <label class="YesNo"> No </label>
</div>
存储在一个名为 a.cshtml
的文件中,我有另一个 html 文件,其中包含如下文件:
<div data-bind="with: testConditions()[0]">
<h4>Test conditions</h4>
@Html.Partial("~/a.cshtml")
</div>
<div data-bind="with: testConditions()[1]">
<h4>Second test conditions</h4>
@Html.Partial("~/a.cshtml")
</div>
在我的 js 文件中我有:
this.testConditions = ko.observableArray([
{ condition: "Yes" },
{ condition: "Yes" }
]);
所以问题是我如何区分单选按钮,就像上面的代码一样,我表单中的所有单选按钮都具有相同的名称,并且基本上在 div 中选择其中一个,包括 testConditions()[0]
影响 div 中的其他人,包括 testConditions()[1]
,反之亦然。
我想在数组中添加第二个属性,例如:
this.testConditions = ko.observableArray([
{ condition: "Yes", itemname: "group1" },
{ condition: "Yes", itemname: "group2" }
]);
并在 html 中使用它,例如:
<div>
<input type="radio" value="Yes" data-bind="checked: condition, name: itemname"> <label class="YesNo"> Yes </label>
<input type="radio" value="No" data-bind="checked: condition, name: itemname"> <label class="YesNo"> No </label>
</div>
然而,它没有用。有什么想法吗?
尝试这样分组
<input type="radio" name="radiobutton[0]" value="a"><br>
<input type="radio" name="radiobutton[0]" value="b"><br>
<input type="radio" name="radiobutton[0]" value="c"><br>
<br>
<input type="radio" name="radiobutton[1]" value="x"><br>
<input type="radio" name="radiobutton[1]" value="y"><br>
<input type="radio" name="radiobutton[1]" value="z">
提交表单时
单选按钮[0] = "a";
单选按钮[1] = "x";
或
尝试 jsfiddle
我找到了答案:
<div>
<input type="radio" value="Yes" data-bind="checked: condition, attr: { 'name': itemname}"> <label class="YesNo"> Yes </label>
<input type="radio" value="No" data-bind="checked: condition, attr: { 'name': itemname}"> <label class="YesNo"> No </label>
</div>