简体   繁体   English

PHP更改选择框选项

[英]php to change select box option

I am using php and trying to change a option using php my code looks like this: 我正在使用php并尝试使用php更改选项,我的代码如下所示:

<?php
$ProductName = 'test2';
?>

<select id="product" name="product" value=<? echo $ProductName; ?> >
   <option value="test1">Option 1</option>
   <option value="test2">Option 2</option>
   <option value="test3">Option 3</option>
   <option value="test4">Option 4</option>
</select>

however the above does not change the option. 但是以上内容并未更改该选项。

any Ideas? 有任何想法吗?

There is a couple of ways to do this. 有两种方法可以做到这一点。 First off the HTML you are looking for is: 首先,您要查找的HTML是:

<option value="test2" selected="selected">Option 2</option>

I prefer the Javascript/jQuery method myself as it doesn't clutter the hell out of my code. 我自己更喜欢Javascript / jQuery方法,因为它不会使我的代码杂乱无章。 After the select HTML: select HTML之后:

$("#product option[value='<?php echo $productName;?>']").attr('selected','selected');

you have to something like this: 您必须执行以下操作:

<select id="product" name="product" value="" >
    <option value="test1"<? if($ProductName == "test1") echo " selected"; ?>>Option 1</option>
    <option value="test2"<? if($ProductName == "test2") echo " selected"; ?>>Option 2</option>
    <option value="test3"<? if($ProductName == "test3") echo " selected"; ?>>Option 3</option>
    <option value="test4"<? if($ProductName == "test4") echo " selected"; ?>>Option 4</option>
</select>

but that would be good to have the values and names of options in an array and do this check in a loop 但这最好在数组中包含选项的值和名称,然后在循环中进行检查

You need to add the selected attribute to the <option> element. 您需要将selected属性添加到<option>元素。 You might consider something like this: 您可能会考虑这样的事情:

<?php
$options = array('test1' => 'Option 1',
                 'test2' => 'Option 2', ...);

$selected = 'test1';
?>

<select id="product" name="product">
   <?php foreach ($options as $value => $text): ?>
      <option value="<?=$value?>"<?=$value == $selected ? ' selected' : ''?>>
        <?=$text?>
      </option>
   <?php endforeach; ?>
</select>

When using a selectbox, the correct HTML to do this would be something like this: 使用选择框时,正确的HTML格式如下所示:

<select id="product" name="product">
   <option value="test1" selected>Option 1</option>
   <option value="test2">Option 2</option>
   <option value="test3">Option 3</option>
   <option value="test4">Option 4</option>
</select>

So add the "selected" word at the end of the value you want to select. 因此,在要选择的值的末尾添加“选定”字样。

In PHP you could do it like this easily: 在PHP中,您可以轻松地做到这一点:

<select id="product" name="product">
<?php
$options = array(1 => 'Option 1', 2 => 'Option 2', 3 => 'Option 3');
foreach ($options as $key => $value) { 
   echo '<option value="' . $key . '"' . ($key == $_GET["test"] ? ' selected="selected"' : '') . '>' . $value . '</option>';
} ?>
</select>

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

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