2013-04-08 80 views
0

我有以下形式的项目:使用jQuery来填充ValidatorHookupControl

Medications:  
<asp:RadioButtonList ID="meds" runat="server" RepeatDirection="Horizontal"> 
    <asp:ListItem Value="1">Yes (list below)</asp:ListItem> 
    <asp:ListItem Value="0">No</asp:ListItem> 
</asp:RadioButtonList> 

Medication List: 
<asp:CustomValidator ID="val_medsList" runat="server" ClientValidationFunction="check_medsList" 
    OnServerValidate="val_medsList_ServerValidate" ValidationGroup="GroupSave" ValidateEmptyText="true" 
    ErrorMessage="required" ControlToValidate="medsList" EnableClientScript="true"> 
</asp:CustomValidator><br /> 
<asp:TextBox ID="medsList" CssClass="jQueryMedsListTarget" TextMode="MultiLine" runat="server" 
    Width="500" MaxLength="500" Wrap="true" Rows="3" /> 

的想法是,如果选择“是”的文本框需要填写,如果选择“否”的情况正好相反。为了有“是” /“否”选项触发我用“ValidatorHookupControl”这样的自定义的验证:

ValidatorHookupControl(document.getElementById('meds_0'), document.getElementById('val_medsList')); 
ValidatorHookupControl(document.getElementById('meds_1'), document.getElementById('val_medsList')); 

这对我的作品,但是这会成为恼人的,当我有两个以上的选项。我创建通过所有的选项下面的循环,但它似乎没有工作(“是”和“否”不触发自定义的验证):

$(document).ready(function() { 
    hookupRadioButtonListToVal($('input[id^=meds_]'), $('#val_medsList')); 
}); 
function hookupRadioButtonListToVal(rbl, validator) { 
    $(rbl).each(function() { 
     ValidatorHookupControl($(this), $(validator)); 
    }); 
} 

我以为我不会返回正确的元素类型为$(this)$(validator),但不知道该从哪里去。

+0

'ValidatorHookupControl'采用DOM元素,而不是jQuery元素。把'($(this),$(validator))'改成'(this,validator)'。可能还有其他问题;这是我第一次看到。 – jbabey 2013-04-08 20:04:29

回答

1

最终溶液最终成为

function hookupRadioButtonListToVal(rbl, validator) { 
    $(rbl).each(function() { 
     ValidatorHookupControl(this, $(validator)[0]); 
    }); 
} 

这使用了jbabey与一个调整到验证部分共享的想法。不幸的是,如果我只是打电话ValidatorHookupControl(this, validator);,我会得到TypeError: val.style is undefined