-3

を使用したjQuery検証スクリプトがありますjquery.validate.js

私が必要とするのは、ユーザーが [はい] を選択したときです - 追加情報が必要です (必須フィールド)。ユーザーが [いいえ] を選択した場合 - 続行します。

これを機能させるために長い間試みましたが成功しませんでした-追加

助けてくれてありがとう!

これが私のPHPコードです:

// If form type is radio, display as a radio button with yes/no answers
            if ($form_type == 'radio')
            {
            echo "<input type=\"radio\" name=\"screen_184\"  value=\"Yes\" checked /> <label>Yes</label> <input type=\"radio\" name=\"screen_184\"  value=\"No\" /> <label>No</label><br /><br />"; 

                // If from type is radio and additional information is required, display a textarea 
                if ($extra_info == 'Yes')
                {
                                   if ($row['required'] == 'Yes') {$class = 'required';} else {$class = 'input_field';}
                echo "<textarea class=\"$class\" maxlegth=\"500\" name=\"screentext_184\" /></textarea></li><br /><br />\n";
            }
4

1 に答える 1

2

jQueryについて質問するときは、PHPではなくjQueryコードを表示する必要があります。

ただし、jQuery Valdiateプラグイン内で、required別のフィールドの状態に基づいて1つのフィールドの条件を設定するには、次のようinputに使用dependsします...

jQuery

$(document).ready(function () {

    $('#myform').validate({ // initialize the plugin
        rules: {
            "screentext_184": {
                required: {
                    depends: function(element) {
                        return $("input[name='screen_184'][value='Yes']").is(":checked");
                    }
                }
            }
        }
    });

});

HTML

<form id="myform"> 
    Yes: <input type="radio" name="screen_184" value="Yes" />
    No: <input type="radio" name="screen_184" value="No" checked="checked" />
    <textarea name="screentext_184"></textarea>
    <input type="submit" />
</form>

作業デモ: http: //jsfiddle.net/qjhv6/

デモのtext input場合、「はい」radioがチェックされるまで、フィールドはオプションです。


編集:ライリーによるコメントによると、これはさらに単純化することができます:

$(document).ready(function () {

    $('#myform').validate({ // initialize the plugin
        rules: {
            "screentext_184": {
                required: "input[name='screen_184'][value='Yes']:checked"
            }
        }  
    });

});

デモ: http: //jsfiddle.net/txryf/

于 2013-02-05T17:22:09.767 に答える