附加带有html代码的字符串

时间:2011-03-31 19:06:10

标签: php javascript html onchange

我正在尝试构建一个片段,稍后会将其插入到更大的代码中。

到目前为止,每个人都工作正常,但有一个问题: 我还没弄明白如何实现ONCHANGE部分。 成功检测到该值,但我只是没有得到一个很好的index.php?day = 23424234组合。我想这是关于转义字符的东西吗?

有人会帮助我吗?

    $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 value=' . $ts . '>' . $tempDay . '</option>';
    }
    $dayChoser.='</select> </form>';

5 个答案:

答案 0 :(得分:3)

这更像是一个Javascript语法问题。 index.php?day=部分应该是一个字符串,this.之后的所有内容都是表达式。

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

HTML属性中JS的引号只需要\ escaping,因为PHP的外引号已经是单引号。

答案 1 :(得分:0)

尝试更改

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

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

答案 2 :(得分:0)

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

您需要内联javascript中的引号中的路径,然后让它连接所选的值。

由插入符号

表示的更改

答案 3 :(得分:0)

您缺少要设置的位置值周围的引号。

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

';

答案 4 :(得分:0)

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