简体   繁体   English

使用JavaScript选择等于php变量的下拉值

[英]Select dropdown value equal to php variable using javascript

I'm trying to select the value, of the dropdown called "Type", that's equal to the value of the PHP variable called "$Type" by using Javascript. 我试图使用Javascript选择下拉列表“ Type”的值,该值等于名为“ $ Type”的PHP变量的值。 I know there is a "selectindex" funciton in JS but "$Type" doesn't contain numbers only text. 我知道JS中有一个“ selectindex”功能,但“ $ Type”不只包含数字文本。

I tried to use a loop I found on this site, but that didn't work for me. 我尝试使用在此站点上找到的循环,但这对我不起作用。

PHP: PHP:

while(list($ArtikelID, $Type) = mysql_fetch_array($result))
{
  $arid = $ArtikelID;
  $te = $Type;
}

HTML + JS: HTML + JS:

<form name="send" method="post" action="editartticlesdef.php">
    articlenumber: </br>
    <input readonly="readonly" name="ArticleID" value=<?php echo $arid ?>></br>
    Type: </br>
    <select name="Type" id="Type">
        <option value="Article">Article</option>
        <option value="Code">Code</option>
        <option value="News">News</option>
        <option value="Project">Project</option>
    </select></br>
<script>
    window.onload = function SelectType() 
    {
        var sel = document.getElementById('Type');
        var val = document.getElementById('<?php echo $te; ?>');
        for(var i = 0, j = sel.options.length; i < j; ++i) 
        {
            if(sel.options[i].innerHTML === val) 
            {
               sel.selectedIndex = i;
               break;
            }
        }
    }
</script>

</form>

I'm not using Ajax or Jquery, so that's why I tried to use the loop. 我没有使用Ajax或Jquery,所以这就是我尝试使用循环的原因。 But it doesn't work for some reason. 但是由于某种原因,它不起作用。 The dropdown still selects the first option on default when loaded. 下拉菜单在加载时仍会默认选择第一个选项。

you can do it this way, using PHP. 您可以使用PHP以这种方式进行。

<?php
$listArticle = array(
                'Article',
                'Code',
                'News',
                'Project'
            );
?>
<select name="Type" id="Type">
    <?php    
    foreach($listArticle as $article)
    {
        $selected = '';
        if($article == $te)
        {
            $selected = 'selected="selected"';
        }

        echo '<option '.$selected.'>'.$article.'</option>';
    }
    ?>
</select></br>

The index referred to in selectedIndex for a select menu is an integer referring to the option item index in the menu - not the value contained therein. select菜单中selectedIndex引用的index是整数,引用菜单中选项项的索引-而不是其中包含的值。

For the javascript, try: 对于javascript,请尝试:

 if( sel.options[ sel.options.selectedIndex ].value === val ){
      sel.selectedIndex = i;
      break;
 } 

You don't need the for loop at all, instead do (based on this ): 您根本不需要for循环,而需要这样做(基于this ):

window.onload = function SelectType() 
    {
        var sel = document.getElementById('Type');
        var val = document.getElementById('<?php echo $te; ?>');
        sel.value = val;
    }

This require the val to be presented in the options. 这要求在选项中显示val。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM