【问题标题】:"selected" in drop down menu using php使用php在下拉菜单中“选择”
【发布时间】:2013-06-22 09:36:56
【问题描述】:
$options1 = array( 1=>'= Equals', '≠ Does not Equal', '> Is greater than', '≥ Is greater than or equal to', '< Is less than', '≤ Is less than or equal', '∋ Contains', '∌ Does not contain');

<select name="entry_id_selector[]">';
foreach ( $options1 as $i1=>$opt1 ) : 
echo '<option value="' .$i1 .'"'
.($i1 == $entry_id_selector_topic) .'? "selected" : "">'
.$opt1 .'</option>';
endforeach;
echo '</select>';

$entry_id_selector_topic = $_POST['entry_id_selector'];

这部分代码有什么问题.($i1 == $entry_id_selector_topic) .'? "selected" : ""

默认值为= Equals

例如,用户选择&gt; Is greater than 然后点击发布按钮,页面重新加载。我希望在下拉菜单中选择并显示值&gt; Is greater than hovewer selected 是默认值。

更改为 .($i1 == $entry_id_selector_topic) .'? selected="selected" : ""&gt;' 始终从数组 ∌ Does not contain 中选择最后一项

更新。最后得到解决方案。起初不明白为什么$entry_id_selector_topic[0] 并不总是显示来自$options1 的第一项。

但是当我从下拉菜单中选择一些值并单击提交按钮时,$entry_id_selector_topic = $_POST['entry_id_selector']; 唯一存在的值是我从下拉菜单中选择的值。所以在$entry_id_selector_topic 中有一个新数组,其中只有一个项 [0]。

现在看来问题很清楚了。

【问题讨论】:

    标签: php


    【解决方案1】:

    此代码有效

    echo '<td><select name="entry_id_selector[]" onmousedown="SetWidthToAuto(this)">';
    foreach ( $options1 as $i1=>$opt1 ) : 
    echo '<option value="' .$i1 .'"'
    .(($i1 == $entry_id_selector_topic[0])? 'selected' : "") .'>'
    .$opt1 .'</option>';
    endforeach;
    echo '</select></td>';
    

    只有我对$entry_id_selector_topic[0]没有解释

    【讨论】:

      【解决方案2】:

      我认为应该是:

      ($i1 == $entry_id_selector_topic)?"selected" : ""
      

      如果该条件为真,它将返回 "selected" ,否则将返回 "" 。

      【讨论】:

      • 试过这样的代码echo '&lt;option value="' .$i1 .'"' .($i1 == $entry_id_selector_topic)? "selected" : ""&gt; .$opt1 .'&lt;/option&gt;'; 什么都不显示(得到空白页)。好像我把' 放错地方了..
      【解决方案3】:
        echo '<option value="'.($i1 == $entry_id_selector_topic) ? "selected" : "".'>'.$opt1 .'</option>';
      

      试试这个

      【讨论】:

      • 不显示$opt1。单击时打开空白下拉菜单(没有值的菜单)
      • 试试isset()功能
      • 唯一的问题是为什么$entry_id_selector_topic[0] 并不总是显示数组中的第一项?
      • $entry_id_selector_topic[0]--只有选中的值
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多