【问题标题】:Appending a string with html-code用 html 代码附加一个字符串
【发布时间】:2011-03-31 19:06:09
【问题描述】:

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

到目前为止一切正常,但有一个问题: 我还没有弄清楚如何实现 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>';

【问题讨论】:

  • 什么是 $tageArray 变量?猜一个数组,对吧?
  • @Wh1T3h4Ck5 - 我认为 $tageArray 可能是一头大象。 :P
  • 可能是任何数据类型,甚至没有声明,我不明白它是什么,所以我只是猜测。因为已经在foreach中使用过,不代表它是数组类型或者可能我什么都没看到。

标签: php javascript html onchange


【解决方案1】:

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

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

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

【讨论】:

  • 你还是忘了把引用的文字分开。
【解决方案2】:

尝试改变

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

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

【讨论】:

    【解决方案3】:
    <select ONCHANGE="location = \'index.php?day=\'+this.options[this.selectedIndex].value;">';
                                 ^^              ^^^
    

    您需要在内联 javascript 中用引号括起来的路径,然后让它连接选定的值。

    插入符号表示的变化

    【讨论】:

      【解决方案4】:

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

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

      ';

      【讨论】:

        【解决方案5】:
         $dayChoser = ' <form name="day"><select ONCHANGE=\'location.href="index.php?day="+this.value;\'>';
        

        【讨论】:

          猜你喜欢
          • 2013-05-08
          • 2013-02-09
          • 1970-01-01
          • 2016-11-14
          • 1970-01-01
          • 2021-12-20
          • 2014-04-21
          • 2011-05-02
          • 1970-01-01
          相关资源
          最近更新 更多