1

I have 2 functions that makes menu links active/inactive:

function disableCtrl() {
    echo 'style="background: #999999;" onclick="return false;"';
}
function enableCtrl() {
    echo ' ';
}

I'm using them like this:

<li><a href="#" <?php if (@$_POST['submit']){disableCtrl();} if (@$_GET['guest'] == "add") {enableCtrl();} ?> >MenuButton</a></span></li>

The only problem I have is that menu links must be disabled by default (when the page is loaded).

When I tried to write <?php disableCtrl(); if (@$_POST['submit'])..., the button became perma-disabled.

Is there a way to make it work, maybe with JavaScript or something else?

4

3 回答 3

1

you need to put the 'guest' check as if and the default(disabled)mode as else.

   <li><a href="#" <?php if ($_GET['guest']== "add"){enableCtrl();} else {disableCtrl();} ?>>MenuButton</a></li>

Or with a ternairy (= short if/else-> [condition ? ifTrue : ifFalse] )

<li><a href="#" <?php $_GET['guest']=="add" ? enableCtrl() : disableCtrl() ?>>MenuButton</a></li>

In combination with disabled (which is made for this exact situation):

<li><a href="#" <?=($_GET['guest']=="add" ? 'disabled="disabled"' : '')?>>MenuButton</a></li>
于 2013-09-22T10:55:58.237 回答
0

If you want something to vary, say, with every load of a page, use a server-side technology such as php. If you want it to vary within one page load, I.e. in response to user interaction, use a client-side technology I.e. JavaScript.

To attempt to answer the original question, or at least give you some pointers, you want something a bit like:

<ul id="menu" style="display: none;"><li><a href="">...</a></li>...</ul>
<a id="btn" href="#">disable menu</a>

<script>
document.getElementById('btn').addEventListener('click', function() {
    document.getElementById('menu').setAttribute('style', 'display: block');
}
</script>

That's not perfect (in practice, for one thing, you don't want to just point your link to "#" since that won't do anything for those with javascript disabled) but it's a pointer in the right direction. The jQuery equivalent would be something like:

<script>
$('#btn').click(function() { $('#menu').hide(); });</script>
</script>

if that's more appealing, check out jQuery.

于 2013-09-22T10:50:08.533 回答
0

Keep in mind that php is server side so you can use printf to put your javascript into your page.

printf("<script>Here is my javascript code</script>");

I do this a lot with the mapping services. For example reading in locations from a database and making markers:

    public function MarkerGoogle($s)
    {
        $str  = sprintf("\r\n   var amkr%d = new google.maps.Marker();\r\n", $s);

        // More php code here …
于 2015-10-19T22:31:41.633 回答