从下拉选项中显示基于 onchange 的 div,帮助

发布于 2024-10-19 06:15:49 字数 2037 浏览 5 评论 0原文

我尝试在

中显示一些信息,如下所示:

    <div id="show_details" style="'display:block;' : 'display:none;'"> SHOW me
    </div>

通过从下拉选项中选择,例如

         <?php if ($books == 'art') { ?>
          <option value="art" selected="selected" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } else { ?>
          <option value="art" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } ?>

,完整代码如下,

    <tr>
      <td>Book Option</td>
      <td>
      <select name="books">
        <?php  foreach ($others as $other) { ?>
          <?php if ($other == $other['other']) { ?>
          <option value="<?php echo $other['other']; ?>" selected="selected"><?php echo $other['title']; ?></option>
          <?php } else { ?>
          <option value="<?php echo $other['other']; ?>"><?php echo $other['title']; ?></option>
          <?php } ?>
          <?php } ?>

          <?php if ($books == 'art') { ?>
          <option value="art" selected="selected" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } else { ?>
          <option value="art" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } ?>
        </select></td>
    </tr>
    <div id="show_details" style="'display:block;' : 'display:none;'"> SHOW me
    </div>

有没有办法解决这个问题?

I try to show some information inside of <div> as following:

    <div id="show_details" style="'display:block;' : 'display:none;'"> SHOW me
    </div>

by choose from drop down option e.g.

         <?php if ($books == 'art') { ?>
          <option value="art" selected="selected" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } else { ?>
          <option value="art" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } ?>

and the full code as following,

    <tr>
      <td>Book Option</td>
      <td>
      <select name="books">
        <?php  foreach ($others as $other) { ?>
          <?php if ($other == $other['other']) { ?>
          <option value="<?php echo $other['other']; ?>" selected="selected"><?php echo $other['title']; ?></option>
          <?php } else { ?>
          <option value="<?php echo $other['other']; ?>"><?php echo $other['title']; ?></option>
          <?php } ?>
          <?php } ?>

          <?php if ($books == 'art') { ?>
          <option value="art" selected="selected" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } else { ?>
          <option value="art" id="art_indicator" onchange="(this.selected) ? $('#show_details').css('display','block') : $('#show_details').css('display','none');">Art</option>
          <?php } ?>
        </select></td>
    </tr>
    <div id="show_details" style="'display:block;' : 'display:none;'"> SHOW me
    </div>

Is there some way to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

衣神在巴黎 2024-10-26 06:15:49
<script type="text/javascript">

function showDiv()
{
  // hide any showing
  $(".details_div").each(function(){
      $(this).css('display','none');
  });

  // our target
  var target = "#details_" + $("#test_select").val();                                                                                                                                

  // does it exist?
  if( $(target).length > 0 )
  {
    // show it
    $(target).css('display','block');
  }
}

$(document).ready(function(){

  // bind it
  $("#test_select").change(function(){
    showDiv();
  });

  // run it automagically
  showDiv();
});


</script>
<style>
.details_div
{
  display:none;
}
</style>
{/literal}

<select id="test_select">
  <option value="">- Select - </option>
  <option value="book" selected="selected">Books</option>
  <option value="movie">Movie</option>
</select>

<div id="details_book" class="details_div">BOOK DETAILS</div>
<div id="details_movie" class="details_div">MOVIE DETAILS</div>
<script type="text/javascript">

function showDiv()
{
  // hide any showing
  $(".details_div").each(function(){
      $(this).css('display','none');
  });

  // our target
  var target = "#details_" + $("#test_select").val();                                                                                                                                

  // does it exist?
  if( $(target).length > 0 )
  {
    // show it
    $(target).css('display','block');
  }
}

$(document).ready(function(){

  // bind it
  $("#test_select").change(function(){
    showDiv();
  });

  // run it automagically
  showDiv();
});


</script>
<style>
.details_div
{
  display:none;
}
</style>
{/literal}

<select id="test_select">
  <option value="">- Select - </option>
  <option value="book" selected="selected">Books</option>
  <option value="movie">Movie</option>
</select>

<div id="details_book" class="details_div">BOOK DETAILS</div>
<div id="details_movie" class="details_div">MOVIE DETAILS</div>
以歌曲疗慰 2024-10-26 06:15:49

看起来您正在使用 jquery,所以我首先建议从 html/php 中删除所有 javascript,并将其放在单独的部分或文件中。

然后,您可以简单地执行以下操作:

$("#art_indicator").change(function() {
  // do what you want to do  
});

此外,#show_details 的 html 似乎是错误的:

style="'display:block;' : 'display:none;'"

没有有效的样式声明。

It looks like you're using jquery so I would first suggest to remove all javascript from the html / php and put it in a separate section or file.

You can then simply do stuff like:

$("#art_indicator").change(function() {
  // do what you want to do  
});

Also, the html for #show_details seems to be wrong:

style="'display:block;' : 'display:none;'"

is no valid style declaration.

吖咩 2024-10-26 06:15:49

我认为 没有注册 onchange 事件。您想要的是使用其父

<script type='text/javascript'>
  function selOnChange() {
    if ($('#selectList').value == "art" {
      $("#show_details").css('display', 'block');
    } else {
      $("#show_details").css('display', 'none');
    }
  }

现在您需要有一个默认的“未选择”选项。我这里有 value="-1"
当选择列表发生变化时,如果其值为#art_indicator的值,则将显示该div。否则,div 将不会显示。

<select id='selectList' name="books" onchange="selOnChange()">
  <option value="-1">Select something</option>
  <option id="#art_indicator" value="art">Art choice...</option>
  <?php // list all your other options ?>
</select>

I don't think <option> registers the onchange event. What you want instead is to use its parent <select onchange=''> to define the change event.

<script type='text/javascript'>
  function selOnChange() {
    if ($('#selectList').value == "art" {
      $("#show_details").css('display', 'block');
    } else {
      $("#show_details").css('display', 'none');
    }
  }

Now you need to have a default "unselected" option. I have it here with value="-1"
When the select list changes, if its value is the value of #art_indicator, the div will be shown. otherwise, the div will not be shown.

<select id='selectList' name="books" onchange="selOnChange()">
  <option value="-1">Select something</option>
  <option id="#art_indicator" value="art">Art choice...</option>
  <?php // list all your other options ?>
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文