2011-03-31 33 views
2

我正在嘗試構建一個代碼片段,稍後會將其插入一段代碼中。附加一個帶有html代碼的字符串

到目前爲止各種工作都很好,但有一個問題: 我還沒有想出如何實現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>'; 
+0

什麼是$ tageArray變量?猜猜一些數組,對嗎? – Wh1T3h4Ck5 2011-03-31 19:11:21

+2

@ Wh1T3h4Ck5 - 我認爲$ tageArray可能是一頭大象。 :P – 2011-03-31 19:14:39

+0

可能是任何數據類型,甚至沒有聲明,我不知道它是什麼,所以我只是猜測。因爲它已經在foreach中使用過,並不意味着它是一個數組類型,或者我沒有看到任何東西。 – Wh1T3h4Ck5 2011-03-31 19:28:33

回答

3

這更像是一個Javascript語法問題。 index.php?day=部分應該是一個字符串,並且this.之後的所有內容都是一個表達式。

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

對於HTML屬性中的JS引號只需要\轉義,因爲PHP的外部引號已經是單引號。

+0

你仍然忘記打破引用的文字。 – 2011-03-31 19:18:05

+0

呃,沒關係..對不起:) – 2011-03-31 19:26:45

0

嘗試改變

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

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

非常感謝! – Nemo 2011-03-31 19:43:15

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

您需要內引號的路徑在線JavaScript,然後把它串聯選擇的值。

通過改變插入符

+0

非常感謝! – Nemo 2011-03-31 19:42:19

0

你缺少你想周圍設置位置值引號表示。

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

';

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

非常感謝! – Nemo 2011-03-31 19:42:45

相關問題