Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据JS中的单选按钮列表设置textbox的maxLength_Javascript_Asp.net - Fatal编程技术网

Javascript 如何根据JS中的单选按钮列表设置textbox的maxLength

Javascript 如何根据JS中的单选按钮列表设置textbox的maxLength,javascript,asp.net,Javascript,Asp.net,我在ASP.Net中有一个单选按钮列表,下面有一个文本框。我希望能够根据单选按钮列表中选择的内容更改文本框的最大长度。我什么都试过了,但我显然错过了什么 这是我的JS:- function ValidateCCN() { var x = document.getElementById('RadioButtonListPayment'); var y = document.getElementById('TextBoxCCN'); if (x.selectedIndex

我在ASP.Net中有一个单选按钮列表,下面有一个文本框。我希望能够根据单选按钮列表中选择的内容更改文本框的最大长度。我什么都试过了,但我显然错过了什么

这是我的JS:-

function ValidateCCN() {
    var x = document.getElementById('RadioButtonListPayment');
    var y = document.getElementById('TextBoxCCN');

    if (x.selectedIndex = "Visa")
        { y.maxLength = '5'; }
    else
        { y.maxLength = '2';}
}
这是我的HTML/ASP

<asp:Label ID="LabelCard" CssClass="labels" runat="server" Text="Select a payment option: "></asp:Label><br />
<asp:RadioButtonList ID="RadioButtonListPayment" runat="server" onchange="ValidateCCN();">
    <asp:ListItem Value="Visa">Visa</asp:ListItem>
    <asp:ListItem Value="MasterCard">MasterCard</asp:ListItem>
    <asp:ListItem Value="Discover">Discover</asp:ListItem>
    <asp:ListItem Value="American Express">American Express</asp:ListItem>
</asp:RadioButtonList>

<asp:Label ID="LabelCCN" CssClass="labels" runat="server" Text="Credit Card Number: "></asp:Label><br />
<asp:TextBox ID="TextBoxCCN" CssClass="textbox" runat="server"></asp:TextBox>
错误:-
else语句不起作用

您有几个问题。您将在ListItems中专门设置GroupName,然后通过javascript中的GroupName循环到所选的一个

ASPX:

JS


此外,在if语句中应该使用比较运算符==而不是赋值运算符=。

在if语句中使用赋值运算符而不是比较运算符。更改此项:

if (x.selectedIndex = "Visa")
对此

if (x.selectedIndex == "Visa")

您当前的解决方案中有什么不起作用?您的JS文件中是否有?如果是这样,那就是你的问题。。。这段代码是ASP.net代码我已经尝试过x.value,并根据您的建议再次尝试过,if语句跳过了第一个条件,即使它是令人满意的。必须按名称而不是id在页面上找到按钮。上面更新了示例代码
if (x.selectedIndex = "Visa")
if (x.selectedIndex == "Visa")
**js**

<script>
$(document).ready(function(){
    $('input[name="theName"]').change(function(){
        if($('#option-1').prop('checked')){
            alert('Option 1 is checked!');
            $('#name').attr('maxlength', '13');
        }else{
            alert('Option 2 is unchecked!');
            $('#name').attr('maxlength', '5');
        }
    });
});
</script>



**html**
 <body>
<input type="radio" name="theName" value="1" id="option-1">no
<input type="radio" name="theName" value="2">yes
<br>
<label>name:</label>
<input type="text" id="name"></input>

</body>