动态生成的选项选项的问题

时间:2013-07-09 14:37:56

标签: jquery select options selectedvalue

所以我有一个简单的页面(html),使用jquery我使用php文件加载到select中。 代码如下: HTML:

<div data-role="page" id="pg_invoicedtl">
  <header data-role="header" data-theme="b">
    <a href="javascript:history.go(0)" data-icon="refresh" class="ui-btn-right" data-theme="e">Refresh</a>
    <h1>MyApp</h1>
  </header>

    <div align="center" data-role="content">
        <ul data-role="listview" data-theme="c" data-divider-theme="e" data-count-theme="b" data-inset="true">
            <li data-role="list-divider">
                <h3 class="ui-li-heading">Select product</h3>
            </li>            

            <li>
            <div data-role="fieldcontain">
                <label for="prodselect" class="select">Choose the product:</label>
                <select name="prodselect" id="prodselect">
                </select>
            </div>            
            </li>
        </ul>
    </div>
</div>

我用来填充选择的脚本(当然位于HEAD部分)是:

<script type="text/javascript">
        $(document).ready(function() {
            $.ajaxSetup ({
                cache: false
            });

            var ajaxLoader = '<img src="images/ajax-loader.gif" alt="loading.." />'; 
            var loadUrl = "loadproducts.php";  


            $('#prodselect').toggle('fast', function() {
                $(this).html(ajaxLoader);
                $(this).toggle('fast', function() {
                    $.get(loadUrl, function(data){
                        $('#prodselect').html(data);
                    },'html');                  
                });
            });
            $('#prodselect').selectmenu('refresh');
        });        
</script>    

现在脚本正在调用的php文件(loadproducts.php):

<?php
session_start();
include "/tmp/conexiune.php";

$pql = mysql_query("select * from products order by name");
$isfirst = 0;
while ($prow = mysql_fetch_row($pql)){
    if ( $isfirst == 0){
        echo '<option id="'.$prow['idproducts'].'"value="'.$prow['name'].'" selected="selected">'.$prow['name'].' - '.$prow['pu'].' EUR</option>';
    }
    else
    {
        echo '<option id="'.$prow['idproducts'].'"value="'.$prow['name'].'">'.$prow['name'].' - '.$prow['pu'].' EUR</option>';
    }
    $isfirst++;
}    
?>

首先我必须告诉你上面的代码是有效的。虽然有一个问题......我的问题......:

  • 正如您可能从代码中看到的那样,我希望select的第一个选项被“选中”,但无论我做什么,都不会发生。刷新页面后,选择框为空,如果我想使用鼠标选择列表中的第一个选项,它什么都不做,就好像第一个项目已经被选中所以它不会这样做。

  • 如果我想在列表中选择第二个或其他选项,一切正常。

  • 因此,选择的第一个元素似乎已被选中,但它不会在选择框中显示它的值。

  • 我甚至添加了$('#prodselect')。selectmenu('refresh');没有结果的脚本......

  • 此外,如果我使用非dinamically生成的选项,一切都很完美。因此,对于静态选项,select会从一开始就显示第一个选项。

    我不明白什么是错的。我在HTML之外测试了php文件(没有从脚本中调用它),结果看起来就像一个完美的选择内容,当放入选择TAG时,它显示了完美的结果。但是,当我从剧本中调用它时,有些东西搞砸了。

    我会在这里放一个jsfiddle,但我不认为它适用于php文件......

2 个答案:

答案 0 :(得分:0)

尝试使用===(注3)而不是==

如果这不起作用,请更改:

$isfirst = 0;

$isfirst = 1;

由于0可以在案例中评估为假。

希望这有帮助!

答案 1 :(得分:0)

试试这个:

 $('#prodselect').selectmenu('refresh', true);

加载动态内容后,您需要强制重建文档中提到的select: http://api.jquerymobile.com/selectmenu/#method-refresh

相关问题