【问题标题】:output txt file with selectbox options in php在php中输出带有选择框选项的文本文件
【发布时间】:2010-05-17 12:11:19
【问题描述】:

我试图在我的 php 页面中显示两个选择框,用户可以从第一个选择框中选择选项以将其添加到第二个框,然后单击一个按钮以生成包含第二个选择框中所有选项的文本文件。

有人可以帮忙吗? (请仅限 IE 7)

这是我到目前为止尝试的代码。

<html>
<head>
<script language="JavaScript">
function moveSelectedOption() {
    // Fetch references to the <select> elements.
    var origin = document.getElementById('origin_select');
    var target = document.getElementById('target_select');

    // Fetch the selected option and clone it.
    var option = origin.options[origin.selectedIndex];
    //var copy = option.cloneNode(true);

    // Add the clone to the target element.
    //target.add(copy, null);

 target.options[target.options.length] = new Option(option.text, option.value);
}
</script>
</head>
<body>
<div style="position:absolute;top:10px;left:10px;">
<?php 
        // Make a MySQL Connection
        mysql_connect("localhost", "jpro", "pop") or die(mysql_error());
        mysql_select_db("db1") or die(mysql_error());
        $result = mysql_query("select t1 from results") or die(mysql_error());  
        ?>
        <select id="origin_select" style="height:400px;width:200px;" multiple>
        <?php
        while($row = mysql_fetch_array( $result )) {
        $t1 = $row['T1'];
        ?>
        <option value="<?php echo $t1; ?>"><?php echo $t1; ?></option> 
        <?php
        }
        ?>
    </select>
</div>
<script>
$("#btn1").click (functio() {
var arr= new Array;
$("#target_select option").each (function () {
//arr.push( $(this).val() );
alert (this.val());
});
});
</script>
<div style="position:absolute;top:50px;left:220px;">
<button onclick="moveSelectedOption()">Add</button>
</div>
<div style="position:absolute;top:10px;left:300px;">
<select id="target_select" style="height:400px;width:200px;" multiple>
<option>&nbsp;</option>
</select></div>
<div style="position:absolute;top:50px;left:560px;">
<button id="btn1">Generate and Save .txt file</button>
</div>



</body>
</html>

【问题讨论】:

    标签: php jquery mysql html


    【解决方案1】:

    您的问题似乎很模糊,但这里有一些关于 HTML/JavaScript/Jquery 部分的帮助。它对我有用(也使用多选):

    <html lang="en">
        <head>
            <!-- include jquery here !-->
    
            <script language="JavaScript">
            function moveSelectedOption() {
                // Walk through ALL selected options in origin list
                $("#origin_select option:selected").each(function() {
                    // Clone option and add to target list
                    $("#target_select").append($(this).clone());
                });
            }
    
            // Gets called on document load
            $(function() {
                $("#btn1").click (function() {
                    var arr= new Array;
                    var str = "";
                    var str2 = "";
                    $("#target_select option").each (function () {
                        // You can push the array on an array
                        arr.push( $(this).val() );
                        // or append it to a string
                        str = str + $(this).val() + "\n";
                        // or append it to a string with <BR>s as line-breaks
                        str2 = str2 + $(this).val() + "<BR>";
                    });
                    // Alert the string to the user
                    alert(str);
    
                    // now do something with either one of them...for example open 
                    // a new window and generate list in there as html to copy-and-paste
                    wn = window.open();
                    wn.document.write("<HTML>"+str2+"</HTML>");
                    wn.document.close();
    
                });
            });
            </script>
        </head>
    
        <body>
            <div style="position:absolute;top:10px;left:10px;">
                    <select id="origin_select" style="height:400px;width:200px;" multiple>
                    <!-- I don't use php so I just added some hard-coded options -->
                    <option value="Just an option ID">Just an Option Value</option> 
                    <option value="In your case ID and Value are the same">In your case ID and Value are the same</option> 
                    <option value="Another One">Another One</option> 
                </select>
            </div>
            <script>
            </script>
    
            <div style="position:absolute;top:50px;left:220px;">
                <button onclick="moveSelectedOption()">Add</button>
            </div>
    
            <div style="position:absolute;top:10px;left:300px;">
                <select id="target_select" style="height:400px;width:200px;" multiple>
                    <!-- do you want to start with an empty line? Add your option &nbsp here then ;) -->
                </select>
            </div>
    
            <div style="position:absolute;top:50px;left:560px;">
                <button id="btn1">Generate and Save .txt file</button>
            </div>
        </body>
    </html>
    

    【讨论】:

    • 感谢您的回复,但这在 IE 中不起作用。我猜 IE 不支持克隆
    • clone 是一个 jQuery 函数。您使用的是最新的 jQuery 版本吗?在 IE 8 中它运行良好!
    • 是的,我正在使用来自 http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js 的最新 jquery,我希望在 IE 7 中工作
    • jQuery 是 1.4.2,你能试试吗?我这里没有IE 7测试盒,以后可以在家试试!
    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    相关资源
    最近更新 更多