0

解決済み ... 閉じることができます ...

ドロップダウン メニューと、ドロップダウン メニューで (動的に) 選択された項目に応じて、別の方法で入力するテキスト領域があります。

私のhtml-phpコードは次のとおりです。

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="jquery-2.0.3.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#copyright_titles').change(function () {                
                var isFirstSelected = $("#copyright_titles option:first-child" ).is(':selected');
                var isLastSelected = $("#copyright_titles option:last-child" ).is(':selected');

                if (isFirstSelected) {
                     $('#copyright_text').hide();
                     return;
                }

                $('#copyright_text').attr("readonly",isLastSelected?false:true);


                $.ajax({ type: "GET", url: "copyrights.xml", dataType: "xml", success: function(xml) {

                    $(xml).find('copyright').each(function() {

                      var copyright_title_selected = $("#copyright_titles option:selected").text();
                      var title = $(this).find('title').text();
                      var text = $(this).find('text').text();

                      if (title === copyright_title_selected) {
                          $('#copyright_text').text(text);
                      }

                    });
                  },
                  error: function(request, error, tipo_errore) { alert(error+': '+ tipo_errore); }
                });


                $('#copyright_text').show();
            });
        });
    </script>
</head>
<body>

    <form name="test" action="test.php" method="post">
        <table>
            <tr>
                    <td>Copyright:</td>
                    <td>
                        <select id='copyright_titles'>
                            <?php
                                $xml = simplexml_load_file('copyrights.xml');
                                $i = 0;
                                foreach($xml->copyright as $copyright)
                                {
                                    $i++;
                                    echo '<option value="copyright'.$i.'">'.$copyright->title.'</option>';
                                }
                            ?>
                        </select>
                    </td>
            </tr>
            <tr>
                    <td></td><td>
                        <textarea id='copyright_text' rows="6" cols="65" style="display:none"></textarea>
                    </td>
            </tr>
        </table>
    </form>

</body>

テキストは、次の xml ファイルから取得されます。

<copyrights>
<copyright>
    <title>free</title>
    <text></text>
</copyright>
<copyright>
    <title>copyright1</title>
    <text>text1</text>
</copyright>
<copyright>
    <title>copyright2</title>
    <text>text2</text>
</copyright>
<copyright>
    <title>copyright3</title>
    <text>text3</text>
</copyright>
<copyright>
    <title>other</title>
    <text></text>
</copyright>

4

3 に答える 3

2

デモ

ウルコードHTMLに従って選択リストを変更します。

<select id="selectMe">
   <option value=""></option>
    <option value="option1">option1</option>
    <option value="option2">option2</option>
</select>
<br><br><br>

<textarea name="copyright_text" id="copyright_text" cols="20" rows="7">

</textarea>

JS:

$(document).ready(function () {

    $('#selectMe').change(function () {

        $('#copyright_text').html($( "#selectMe option:selected" ).text());

    })
});
于 2013-10-15T11:35:34.213 に答える
0
   <select name="copyright_titles" id='copyright_titles'>
                            <?php
                                $xml = simplexml_load_file('copyrights.xml');
                                $i = 0;
                                foreach($xml->copyright as $copyright)
                                {
                                    $i++;
                                    echo '<option value=".$copyright->text.">'.$copyright->title.'</option>';
                                }
                            ?>
                        </select>

//jquery 関数

$('#copyright_titles').change(function() { $('#copyright_text').attr('val',$('#copyright_titles').val()); });
于 2013-10-15T11:15:28.953 に答える
0

サーバー側で PHP を使用するよりも、メソッドSELECTを使用してクライアント側でリストを作成するのが最善です。jQuery.Get()XML ファイルを非同期的にロードし、イベント ハンドラーを作成してTEXTAREASELECT.

于 2013-10-15T11:15:37.097 に答える