【解決方法】ラジオボタンが選択されていないときにアラートを表示する


こんにちは、私はラジオボタンを持っています。 ラジオボタンのチェックを外すとアラートが来ますが、ラジオボタンをチェックしてもアラートが来ます

私のコードは

<script type="text/javascript">
function as()
{
<code>//if(!document.getElementById("RadioBtn").checked)
</code>{
alert("check the field");
}
}
</script>

そしてdivで

<asp:RadioButtonList ID="RadioBtn" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
<asp:button Onclientclick="as()" runat="server">

javascriptを使ったバリデーションを使いたいです。

解決策 1

関数を radiobuttonlist のイベントに割り当てている場所はどこですか?
さらに、RadionButtonList には「Checked」プロパティや属性さえないと思います。

選択時にアラートを受け取るには、次の JQuery コードを試してください (同じ RadioButtonList 内の他のラジオ ボタンを選択すると、選択解除が自動的に行われるため)。

$(document).ready(function() {
            $("#RadioBtn input[type='radio']").click(function() { alert('hi'); });
        });

解決策 7

これを試してみませんか..

JavaScript
<script type="text/javascript">
    function as()
    {
        if(!document.getElementById('<%=RadioBtn.ClientID %>_0').checked)
        {
        alert("check the field");
        }
    }
    </script>
ASP.NET
<asp:radiobuttonlist id="RadioBtn" runat="server" repeatdirection="Horizontal" xmlns:asp="#unknown">
            <asp:listitem>Male</asp:listitem>
            <asp:listitem>Female</asp:listitem>
        </asp:radiobuttonlist>
        
        <asp:button id="Button1" runat="server" text="Button" onclientclick="as();" xmlns:asp="#unknown" />

ありがとう

解決策 3

問題は、ASP.NET コード用に生成された HTML コードです。

XML
<table id="RadioBtn" border="0">
            <tr>
                <td>
                    <input id="RadioBtn_0" type="radio" name="RadioBtn" value="Male" /><label for="RadioBtn_0">Male</label>
                </td>
                <td>
                    <input id="RadioBtn_1" type="radio" name="RadioBtn" value="Female" /><label for="RadioBtn_1">Female</label>
                </td>
            </tr>
        </table>
        <input type="submit" name="Button1" value="" onclick="as();" id="Submit1" />

ASP.NET のすべてがクライアント用の HTML にレンダリングされることがわかり、上記の結果が得られます。 上記の RadioButtonList コントロールが他のコントロール内にある場合、またはマスター ページがある場合、テーブルの ID は異なり、document.getElementById(…) を試行すると未定義になります。

JavaScript が ASP.net コード用に生成されたクライアント コードと一致していることを常に確認してください。 右クリック> Webブラウザでソースを表示できます。

それを解決するには、JavaScript コードを以下のコードに置き換えます

C#
<script type="text/javascript">
    function as()
    {
        //Get the client ID of the control
        var tableID = '<%= RadioBtn.ClientID %>';
        var radioButtons = document.getElementsByName(tableID)
        var totalRows = radioButtons.length;
        var radioButton;
        var flagRadioButtonChecked = false;
        for(var rowIndex=0; rowIndex< totalRows; rowIndex++)
        {
            radioButton = radioButtons[rowIndex]
            //Check if it's a radio button
            if(radioButton.type == 'radio')
            {
                //Check if it's checked
                if(radioButton.checked)
                {
                    //current radio button is checked
                    flagRadioButtonChecked=true;
                    //breaking from here will keep reference of checked radio button to radioButton variable
                    break;
                }
            }
        }
        //Check if any radio button is checked
        if(flagRadioButtonChecked)
        {
            alert(radioButton.value + ' is checked...');
        }
    }
    </script>

これが役立つことを願っています。

ありがとう、
ヘマント

解決策 5

こんにちは、とても簡単です

これを試してみてください

HTML
<input id="Radio1" type="radio"  önclick="return alert('Hi friends i m sachin here');" />

解決策 9

$(“#btnWasteBottle_Save”).click(関数 () {

$(document).ready(function() {
$(“#RadioBtn 入力[type=”radio”]”).click(function() { alert(‘‘); });
});

if(“#wasteVal”).val ==”” & ==null{
alert(‘‘);
}
if(“#ingredientMain”).val ==”” & ==null{
alert(‘‘);
}
if(“#ingredientSub”).val ==”” & ==null{
alert(‘‘);
}

それ以外{
false を返します。
}

コメント

タイトルとURLをコピーしました