0

アンケートがあります。30 以上のはい/いいえの質問 (ラジオボタン); いいえを選択した場合、理由を示すテキスト ボックスを表示します。いいえを選択した場合、このテキスト ボックスは必須です。ひとつひとつできない。ワイルドカードとループを使用してこれを行う方法。

<tr>
    <td>1</td>
    <td width="600px">question 1?</td>
    <td width="65px">
        <asp:RadioButton ID="rdM1Y" runat="server" Text="Yes" GroupName="rdM1" />
        <asp:RadioButton ID="rdM1N" runat="server" Text="No" GroupName="rdM1" />
    </td>
    <td><asp:TextBox ID="txtCM1" runat="server" /></td>
</tr>
<tr>
    <td>2</td>
    <td width="600px">question 2?</td>
    <td width="65px">
        <asp:RadioButton ID="rdM2Y" runat="server" Text="Yes" GroupName="rdM2" />
        <asp:RadioButton ID="rdM2N" runat="server" Text="No" GroupName="rdM2" />
    </td>
    <td><asp:TextBox ID="txtCM2" runat="server" /></td>
</tr> 

いいえを選択した場合、それらのテキストボックスが必要であることを検証する必要があります

4

4 に答える 4

0

わかりました、そう:

jQuery をインクルードします (非常に簡単になります):

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

radNoすべての「いいえ」ラジオ ボタンのクラスを追加します。

<asp:RadioButton class="radNo" ID="rdM2N" runat="server" Text="No" GroupName="rdM2" />

txtNoすべてのテキストボックスにクラスを追加:

<asp:TextBox class="txtNo" ID="txtCM2" runat="server" />

スクリプトを追加:

<script type="text/javascript">
$(function(){
    $('.radNo').click(function(){
        var c = $(this).is(':checked');
        $(this).parents('tr').find('txtNo').toggle(c);
        if (c) {
            $(this).parents('tr').find('txtNo').focus();
        }
    });

    // validate that there's text if select NO
    $('.txtNo').bind('blur', function(){
        if ($(this).val().length < 1) {
            $(this).focus(); 
            alert('please provide a reason');
        }
    });       
});
</script>

それはそれを行う必要があります、私がよく理解し、それが役立つことを願っています.

于 2013-10-16T14:10:09.547 に答える
0

生成された HTML - textareaID がradioボタン名と同じであることに注意してください

<table>
    <tr>
    <td>1</td>
    <td width="600px">question 1?</td>
    <td width="65px">
        <label><input type="radio" name="rdM1" /> Yes</label>
        <label><input type="radio" name="rdM1" class="give_reason" /> No</label>
    </td>
    <td><textarea id="rdM1" name="rdM1-reason"></textarea></td>
</tr>
</table>

CSS

textarea {display: none;}

Javascript

$(document).on('click', 'input:radio', function(){
   el = $('#' + this.name); 
   ($(this).hasClass('give_reason'))? el.show() : el.hide(); 
});

デモ

于 2013-10-16T14:10:17.977 に答える
0
$(document).on('click', 'input:radio', function(){
$('table').each(function(){
if($('input[type= radio]:checked').val() == no)) 
{
    $('textbox').show();
}
});

  });
于 2013-10-16T14:38:32.740 に答える
0

最初に no-radiobuttons に共通のクラスを与えます。たとえば、「noRadio」とします。次に、このクラスを使用して、すべてのラジオ ボタンのクリック イベントをリッスンできます。イベント ハンドラー関数内で、対応するテキスト ボックスを見つけて表示できます。私のサンプルコードは以下の通りです:

$(".noRadio").click(function(e) {
    // get id of the clicked radio button
    var id = $(e.target).attr("id");
    // remove "rdM" and "N" from id and get pure question number
    id = id.replace("rdM", "").replace("N", "");
    // select the corresponding text box by its id, e.g. "#txtCM2" if "#rdM2N" selected
    $("#txtCM" + id).show();
});

私は自分のプロジェクトでこのアプローチを試しましたが、うまくいきました。

于 2013-10-16T14:11:22.707 に答える