开发者

Appending a string with html-code

开发者 https://www.devze.com 2023-02-21 19:49 出处:网络
I\'m trying to build a snippet which will later be inserted into a bigger piece of code. Everythings working fine so far, but there\'s one problem:

I'm trying to build a snippet which will later be inserted into a bigger piece of code.

Everythings working fine so far, but there's one problem: I have not figured out how to implement the ONCHANGE-part. The value is detected successfully, but I just don't get a fine index.php?day=23424234 combination. I suppose it's something about escape characters?

Would anyone help me?

    $dayChoser = ' <form name="day">
<select ONCHANGE="location = index.php?day=this.options[this.selectedIndex].value;">
';
    foreach ($tageArray as $ts) {
        $tempDay = date('m/d/Y', $ts);
        $dayChoser.='<option 开发者_如何学Govalue=' . $ts . '>' . $tempDay . '</option>';
    }
    $dayChoser.='</select> </form>';


It's more a Javascript syntax problem. The index.php?day= part should be a string, and everything after the this. is an expression.

$dayChoser = ' <form name="day">
    <select ONCHANGE="document.location = \'index.php?day=\' + this.options[this.selectedIndex].value;">
';

The quotes for JS in the HTML attribute just need \ escaping, because the outer quotes for PHP are already single quotes.


Try changing

<select ONCHANGE="location = index.php?day=this.options[this.selectedIndex].value;">

to

<select ONCHANGE="window.location = \'index.php?day=\' + this.options[this.selectedIndex].value">


<select ONCHANGE="location = \'index.php?day=\'+this.options[this.selectedIndex].value;">';
                             ^^              ^^^

You need the path in quotes within the in-line javascript, then have it concatenate the selected value.

Changes denoted by carets


You are missing quotes around the location value that you want to set.

$dayChoser = ' <form name="day"><select ONCHANGE="location = \'index.php?day=\' + this.options[this.selectedIndex].value + \';\'">

';


 $dayChoser = ' <form name="day"><select ONCHANGE=\'location.href="index.php?day="+this.value;\'>';
0

精彩评论

暂无评论...
验证码 换一张
取 消