duanjue9296 2013-03-01 18:34
浏览 22
已采纳

刷新页面后,请记住所选值

In a page containing a form with select fields, I need to remember the previuos choice (if any) of, for example, the selected state. I want to do it via PHP embedded in JS, where I'm wrong?

<script>
<?php
if(isset( $_GET["state"] )){
$selected_state =  $_GET["state"];
?>
$("#state option[<?php echo $selected_state ?>]").attr("selected", "selected");
<?php } ?>
</script>

Thank you in advance!

  • 写回答

2条回答 默认 最新

  • dongliling6336 2013-03-01 18:45
    关注

    Replace:

    $("#state option[<?php echo $selected_state ?>]").attr("selected", "selected");
    

    With:

    $("#state option[value='<?php echo $selected_state ?>']").attr("selected", "selected");
    

    The problem is with the jQuery selector #state option[SOMETHING] which checks if the element option has an attribute called SOMETHING, where you want to see if the value of an option is equal to SOMETHING so we use #state option[value='SOMETHING'].

    You can find the jQuery selectors docs here: http://api.jquery.com/category/selectors/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM