嵌入在 HTML 中的 JavaScript


JavaScript embedded in HTML

我有一段javascript代码,当进行某个选择时,它会导致一个文本框出现在下拉菜单旁边。它运行良好,直到我将其嵌入 html 中。我不确定为什么它现在不起作用,尽管它可能真的很微不足道。

这是函数,就像现在一样:

$public->html .= '<script type="text/javascript">;
    $(function(){
    // initially check the default value in dd_question
    if($("#dd_question").find("option:selected").val() == "0"){
            $("#other_question").show();
          }else{
            $("#other_question").hide();
          }
        $("#dd_question").change(function() {
          if($(this).find("option:selected").val() == "0"){
            $("#other_question").show();
          }else{
            $("#other_question").hide();
          }
        });
    });
  </script>';

如果能再次工作,那就太好了。谁能看出为什么不是?

我不是php用户,但我可以告诉你,这一行的分号可能不应该在那里。

$public->html .= '<script type="text/javascript">;

浪费代码

<script type="text/javascript">
$(function() {
  $("#dd_question").change(function() {
    var show = $(this).val() == "0";
    if (show) $("#other_question").show();
    else      $("#other_question").hide();
  }).change();
});
</script>