PHP。 如何动态更改HTML选择操作的页面内容?

I have this code:

page.php:

<?php

include ('include.php');

echo = '
<li>
    <select name="selector" class="letter">
        <option value="a" selected="selected">$a</option>
        <option value="b">$b</option>
    </select>
</li>
';

echo = '
<li>
    <select name="a-numbers" class="number a-numbers">
        <option value="$a[1][1]">$a[1][2]</option>
        <option value="$a[2][1]">$a[2][2]</option>
    </select>
    <select name="b-numbers" class="number b-numbers">
        <option value="$b[1][1]">$b[1][2]</option>
        <option value="$b[2][1]">$b[2][2]</option>
    </select>
</li>
';

?>

&

include.php:

<?php

$array = array(
    "a" => array(
        "1" => array("aaa1-value","aaa1-text"),     
            "2" => array("aaa2-value","aaa2-text")      
        ),  
        "b" => array(
            "1" => array("bbb1-value","bbb1-text"),     
            "2" => array("bbb2-value","bbb2-text")      
        )   
    )
);

?>

If .letter>a is selected, .a-numbers list should be echo-printed; and if .letter>b is selected, .a-numbers list should be removed and .b-numbers should be printed.

Of course, I can create this scenario with JS and CSS display:none; however, I can't use display:none here. So I have to remove elements from DOM, but I can't.

Thanks for your attention.

PHP cannot dynamically do any of this on it's own. You will need to either use AJAX, which will actually send a request/response back to the server to populate the div.

Or, you will need to load all possible content with the initial request/response and use javascript to dynamically show/hide the divs in question.

You mention in your question that you cannot use display:none, so you may want to look into making an asychronous call back the webserver to populate the element.

If you just want to remove something from the DOM. jQuery can do it by using

 $.remove("[selector]")

Then if you need to add them you can use jquery prepend or append. Such as:

$("[selector]").append("<!--escaped HTML-->");