drhzc64482 2012-05-15 03:14
浏览 34
已采纳

jquery表示是否未选择选项

I would like to add a small code snip to check out whether an option is selected This usually is easily done if the page is html, now my code snip looks something like this

echo "<td>Language</td>";
echo "<td><select id='language' name='language'>
            <option value=''>Select...</option>
            <option value='en_US'>English(US)</option>
            <option value='en_AU'>English(AU)</option>
            <option value='en_UK'>English(UK)</option>
          </select><span id="info"></span>
      </td>";   

if it is html

<script>
   $('#language').change()
   {
     if('Select...'==$('#language').text())
     {
        $('#info').html("Please select a value...");
     }
   }
</script>

[UPDATE]

I would like to add that the above php source code used to generate html code is put in a form tag that is

echo "<form action='checkdb.php' method='POST'>
    // all above and more
    </form>"

the page checkdb.php used the posted data to update database.

How can I add in the jquery code piece as mentioned in the current file (the file with the html code is being generated) ? I am thinking as people once told me that javascript can't be called in the middle of the php execution and I wonder if I may need to use ajax get function. Please offer some help. Thank you.

  • 写回答

3条回答 默认 最新

  • dongnaosuan5407 2012-05-15 03:35
    关注
    // Listen, and respond to, the change even on our select
    $("#language").on("change", function(e){
      // Set the html content of our info element
      $("#info").html(function(){
        // If the selected option has no value, ask to select a value
        return e.target.value === "" ? "Please select a value." : "" ;
      });
    // Trigger upon page load
    }).trigger("change");
    

    Demo: http://jsbin.com/owaqaz/2/edit

    Note the last line, $.trigger("change") - this will cause the page to immediately populate the #info element if the select element currently has no value.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题