这个javascript、php、显示失败的简单步骤是什么?can';我想不出来
代码如下:这个javascript、php、显示失败的简单步骤是什么?can';我想不出来,javascript,php,css,displaytag,displayobject,Javascript,Php,Css,Displaytag,Displayobject,代码如下: <?php $countrycode=$p["country"]; ?> <select size="1" name="countrycode" id="countrycode" onchange="javascript: if(this.value=='USA') { document.getElementById('us').style.display='block'; } else { document.ge
<?php $countrycode=$p["country"]; ?>
<select size="1" name="countrycode" id="countrycode" onchange="javascript:
if(this.value=='USA') {
document.getElementById('us').style.display='block';
} else {
document.getElementById('us').style.display='none';
}
if(this.value=='DE') {
document.getElementById('de').style.display='block';
} else {
document.getElementById('de').style.display='none';
}
">
<option value="INT" <?php
if($countrycode=='INT') { echo 'selected="selected"'; } ?>
>International</option>
<option value="USA" <?php
if($countrycode=='USA') { echo 'selected="selected"'; } ?>
>USA</option>
<option value="DE" <?php
if($countrycode=='DE') { echo 'selected="selected"'; } ?>
>DE</option>
</select>
>美国
这是丢失的回声(刚刚忘记)
“id=“us”>
还允许:
value=“1”>加拿大
value=“1”>英国
value=“1”>澳大利亚
<?php if($countrycode=='DE') { echo 'display:block;'; } else { echo 'display:none;'; } ?>
“id=”de“>
还允许:
value=“1”>瑞士
value=“1”>奥地利JS Fiddle一个更好的方法示例:
HTML:
国际的
美国
判定元件
美国分部
德迪夫
国际部
JS:
var selectedCountry='';
//var selectedCountry=‘美国’;//用于测试
var divs=$('us,'de,'int');
var countrycode选择器=$(“#countrycode”);
countryCodeSelector.val(selectedCountry);
selectCountry(selectedCountry);
乡村代码选择器
.改变(
函数(){
var code=$(this.val();
选择国家(代码);
}
)
;
功能选择国家/地区(代码){
var selectedID='';
开关(代码){
“美国”案:
selectedID='us';
打破
案例“DE”:
选择edid='de';
打破
案例“INT”:
选择edid='int';
打破
}
divs.hide();
$('#'+selectedID).show();
}
还有进一步的优化工作要做,但这是可行的(例如设置国家值、ID和标题之间的映射。您到底想在这里做什么?看起来您让事情变得比应该的更复杂。如果可能的话,您应该将内容与功能分开。请不要使用内联JavaScript。如您所见,这会使您的代码一团糟。使用document.getElementById('countrycode').addEventListener('change',function(){})
document.getElementById('us')和document.getElementById('de')具有这些ID的元素在哪里?编写所有新的元素将非常耗时。因为这是一个准备好的脚本,它是按原样编写的。我刚刚添加了一个新函数-除了这个微小的代码结果之外,所有功能都可以正常工作。
<?php
if($p["UK"]=='1') { echo 'checked="checked"'; } ?>
<?php
if($p["AU"]=='1') { echo 'checked="checked"'; } ?>
<?php if($countrycode=='DE') { echo 'display:block;'; } else { echo 'display:none;'; } ?>
<?php
if($p["AT"]=='1') { echo 'checked="checked"'; } ?>
<select id="countrycode" name="countrycode">
<option value="INT">International</option>
<option value="USA">USA</option>
<option value="DE">DE</option>
</select>
<div id="us">USA div</div>
<div id="de">DE div</div>
<div id="int">INT div</div>
var selectedCountry = '<? echo $p["country"]; ?>';
//var selectedCountry = 'USA'; // for testing
var divs = $('#us, #de, #int');
var countryCodeSelector = $('#countrycode');
countryCodeSelector.val(selectedCountry);
selectCountry(selectedCountry);
countryCodeSelector
.change(
function() {
var code = $(this).val();
selectCountry(code);
}
)
;
function selectCountry(code){
var selectedID = '';
switch(code){
case 'USA':
selectedID = 'us';
break;
case 'DE':
selectedID = 'de';
break;
case 'INT':
selectedID = 'int';
break;
}
divs.hide();
$('#' + selectedID).show();
}