简体   繁体   English

如何获取select元素内的span元素的ID?

[英]How can I get id of span element inside select element?

My html form has the content below: 我的html表单具有以下内容:

<form name="profile">
 ...
 <select id="province" name="province">
  <span id="provinceWarning" class="alert"></span>
  <option value="">Select Province</option>
  <option value="AB">Alberta</option>
   ...

In my javascript, I'm trying to grab the form's ID to show a message telling the user to choose something in the dropdownlist if they haven't chosen anything and pressed the submit button. 在我的JavaScript中,我试图获取表单的ID,以显示一条消息,告诉用户如果未选择任何内容并按下“提交”按钮,则从下拉列表中选择某项。 The problem is that the javascript block returns undefined. 问题是javascript块返回未定义。

//validate the profile form
function validate(e){

    e.preventDefault();

    var valid=true;


   if(document.profile.province.value == ""){
       document.getElementById('provinceWarning').innerHTML="*Please choose a province*";
       valid=false;
    }

    if(valid){
        alert("Thank You!");
    }

    return valid;

};

You can't have a span tag inside a select tag. select标签内不能包含span标签。 Browser while rendering this HTML would have stripped it off. 浏览器在渲染此HTML时会删除它。

See the demo below (check in your browser's dev tools that browser is stripping off span tag while rendering) 请参阅下面的演示 (在浏览器的开发工具中检查浏览器在渲染时剥离了span标签

 <select id="province" name="province"> <span id="provinceWarning" class="alert"></span> <option value="">Select Province</option> <option value="AB">Alberta</option> </select> 

You need to put the span tag outside the select tag. 您需要将span标签放在select标签之外。

Try this, it comes down and reset once u select and submit. 试试这个,一旦您选择并提交,它就会降下来并重置。

 function submit(){ if(!document.getElementById("province").value){ document.getElementById("provinceWarning").innerHTML="*Please choose a Province*"; valid=false; }else{ document.getElementById("provinceWarning").innerHTML="" valid=true; window.alert("thank you") } } 
 <form name="profile"> <select id="province" name="province"> <option value="">Select Province</option> <option value="AB">Alberta</option> </select> <span id="provinceWarning" class="alert"></span> </form> <button id="btnSubmit" onclick="submit();">Submit</button> 

Problems: 问题:

1- You can not define span in select option then move it out of your select 1-您无法在select选项中定义span ,然后将其移出选择范围

2- To check select value you should getElementById("province") and then check the value 2-要检查选择值,您应该getElementById("province")然后检查该值

3- Testable code can be as below(You can change it as you want): 3-可测试的代码如下所示(您可以根据需要进行更改):

 function func() { var ddl = document.getElementById("province"); var selectedValue = ddl.options[ddl.selectedIndex].value; if (selectedValue == ""){ document.getElementById("provinceWarning").innerHTML="*Please choose a Province*"; valid=false; }else{ alert("Thank You!"); } } 
 <form name="profile"> <div> <span id="provinceWarning" class="alert"></span> </div> <div> <select id="province" name="province"> <option value="">Select Province</option> <option value="AB">Alberta</option> </select> </div> </form> <button id="btnSubmit" onclick="func();">Click Me</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM