-2

我想在 PHP if 中加入一些 JavaScript。如下所示。但是我不能让它正常工作,建议?

    if ($_option->getTitle() == 'Condition') {
            '<script language="javascript">
function displayCondition() {
condition = new Array("Please Select a Condition.","<b>Perfect:</b> Text to explain perfect condition!", "<b>Excellent:</b> Text to explain excellent condition!", "<b>Good:</b> Text to explain good condition!", "<b>Poor:</b> Text to explain poor condition!", "<b>Broken:</b> Text to explain broken condition!");
var getsel = document.getElementById(\'select_'.$_option->getId().'\').value;
document.getElementById("divId").innerHTML = condition[getsel];
}
</script>';
            $extraParams .= ' onchange="opConfig.reloadPrice();displayCondition();"';
        } else if 
            ($_option->getTitle() == 'Original Packaging') {
            $extraParams .= ' onchange="opConfig.reloadPrice();Select(select_'.$_option->getId().',\'divShow\',2);"';
        } else {
            $extraParams .= ' onchange="opConfig.reloadPrice();"';
        }
4

4 回答 4

1

当你混合 PHP 和 HTML(包括 JavaScript)时,你必须注意 PHP 的开始标签:

if ($_option->getTitle() == 'Condition') {
    // go out of PHP
    ?>
    <script language="javascript">
       function displayCondition() {
         condition = new Array("Please Select a Condition.","<b>Perfect:</b> Text to explain perfect condition!", "<b>Excellent:</b> Text to explain excellent condition!", "<b>Good:</b> Text to explain good condition!", "<b>Poor:</b> Text to explain poor condition!", "<b>Broken:</b> Text to explain broken condition!");
         var getsel = document.getElementById('select_<?php echo $_option->getId(); ?>').value;
         document.getElementById("divId").innerHTML = condition[getsel];
       }
    </script>
    <?php
    // back to php
    $extraParams .= ' onchange="opConfig.reloadPrice();displayCondition();"';

您也可以尝试制作大的echo,但这涉及处理引号擒纵机构并且通常更重。

于 2013-03-30T12:44:57.397 回答
0

只要确保你回显<script>.

<?php if (condition) {
  echo '<script> //do stuff </script>';
?>

或者使用控制结构的替代语法将 包含<script>到页面中

<?php if (condition): ?>
  <script> //do stuff </script>
<?php endif; ?>
于 2013-03-30T12:50:55.917 回答
0

你可以这样写

 $extraParams = "";
if ($_option->getTitle() == 'Condition') {
$extraParams .= '<script>...you script </script>';

$extraParams .= ' onchange="opConfig.reloadPrice();displayCondition();"';
    } else if 
        ($_option->getTitle() == 'Original Packaging') {
        $extraParams .= ' onchange="opConfig.reloadPrice();Select(select_'.$_option->getId().',\'divShow\',2);"';
    } else {
        $extraParams .= ' onchange="opConfig.reloadPrice();"';
    }
于 2013-03-30T12:52:39.300 回答
0

在 PHP 中处理大型标记时,您应该使用PHP 的 HEREDOC 。

$script_tage = <<<HEREDOC
<script language="javascript">
    function displayCondition() {
    condition = ["Please Select a Condition.","<b>Perfect:</b> Text to explain perfect condition!", "<b>Excellent:</b> Text to explain excellent condition!", "<b>Good:</b> Text to explain good condition!", "<b>Poor:</b> Text to explain poor condition!", "<b>Broken:</b> Text to explain broken condition!"];
    var getsel = document.getElementById('select_'.{$_option->getId()}).value;
    document.getElementById("divId").innerHTML = condition[getsel];
</script>
HEREDOC;


if ($_option->getTitle() == 'Condition') {
    echo $script_tag;
}

Note- there is no need to escape anything in HEREDOC. To enter variables in the HEREDOC string, just put them in a pair of braces like {$foo}.

于 2013-03-30T12:58:37.700 回答