Javascript 组合元素ID
我正试图让两个标签都显示出来,但不幸的是,其中只有一个标签显示的是城市标签Javascript 组合元素ID,javascript,jquery,Javascript,Jquery,我正试图让两个标签都显示出来,但不幸的是,其中只有一个标签显示的是城市标签 <script type="text/javascript"> $(document).ready(function() { function showDiv(element, pro2) { if (pro2.children("option:selected").val() == 2) element.show(); else element.hide();
<script type="text/javascript">
$(document).ready(function() {
function showDiv(element, pro2) {
if (pro2.children("option:selected").val() == 2) element.show();
else element.hide();
}
var myElement = $("div#pro2");
var mypro2 = $("select#ptype");
$("select").change(function() {
showDiv(myElement, mypro2)
});
$("#ctry").change(function() {
$(".state").hide();
var stateSelect = $("#state_" + $(this).val());
if (stateSelect.length === 0)
$("#state_label" && "#city_label").hide();
else {
$("#state_label" && "#city_label").show();
stateSelect.show();
}
});
});
</script>
$(文档).ready(函数(){
函数showDiv(元素,pro2){
if(pro2.children(“option:selected”).val()==2)元素.show();
else元素。hide();
}
var myElement=$(“div#pro2”);
var mypro2=$(“选择#ptype”);
$(“选择”).change(函数(){
showDiv(myElement,mypro2)
});
$(“#ctry”).change(函数(){
$(“.state”).hide();
var stateSelect=$(“#state"”+$(this.val());
如果(stateSelect.length==0)
$(“#州#标签”和&“#城市#标签”).hide();
否则{
$(“#州#标签”和&“#城市#标签”).show();
stateSelect.show();
}
});
});
HTML代码:
<label id="state_label" style="display:none">State:</label><br />
<label id="city_label" style="display:none">Postal or City:</label>
状态:
邮政或城市:
那是。。。而不是&
的工作方式。在这种情况下,它将返回其右操作数。您需要的是,在选择器中使用逗号:
<script type="text/javascript">
$(document).ready(function() {
function showDiv(element, pro2) {
if (pro2.children("option:selected").val() == 2) element.show();
else element.hide();
}
var myElement = $("div#pro2");
var mypro2 = $("select#ptype");
$("select").change(function() {
showDiv(myElement, mypro2)
});
$("#ctry").change(function() {
$(".state").hide();
var stateSelect = $("#state_" + $(this).val());
if (stateSelect.length === 0)
$("#state_label, #city_label").hide();
else {
$("#state_label, #city_label").show();
stateSelect.show();
}
});
});
</script>
$(文档).ready(函数(){
函数showDiv(元素,pro2){
if(pro2.children(“option:selected”).val()==2)元素.show();
else元素。hide();
}
var myElement=$(“div#pro2”);
var mypro2=$(“选择#ptype”);
$(“选择”).change(函数(){
showDiv(myElement,mypro2)
});
$(“#ctry”).change(函数(){
$(“.state”).hide();
var stateSelect=$(“#state"”+$(this.val());
如果(stateSelect.length==0)
$(“#州#标签,#城市#标签”).hide();
否则{
$(“#州标签,#城市标签”).show();
stateSelect.show();
}
});
});
您不能像现在这样使用&&
运算符一次选择多个元素,您应该将它们包含在一个由逗号分隔的字符串中;试试这个:
if (stateSelect.length === 0)
$("#state_label,#city_label").hide();
else {
$("#state_label,#city_label").show();
stateSelect.show();
}