使用PHP数组进行javascript选择选项

时间:2021-11-24 02:06:23

I mixed the code that I have and from the * related in using select option.

我混合了我的代码和使用select选项相关的*。

The code has javascript and php & I included the link to the plugins just for details.

代码有javascript和php&我包含插件的链接只是为了细节。

So I used php arrays for the select option. I am trying to populate the 2nd selection option which is for the select city.

所以我使用php数组作为选择选项。我正在尝试填充选择城市的第二个选择选项。

I need to use PHP array because of timezone purposes

由于时区的原因,我需要使用PHP数组

使用PHP数组进行javascript选择选项

Problem: Unable to populate the second option(s)

问题:无法填充第二个选项

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery-1.12.3.js"></script>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript" src="json_parse.js"></script>
<script type="text/javascript" src="json_parse_state.js"></script>
<script type="text/javascript" src="cycle.js"></script>

</head>
<body>	
<?php $myvalues = array(
"Japan (日本)"=>"JP",
"Albania (Shqipëri"=>"AL",
"Algeria (‫الجزائر‬‎)"=>"DZ",
"American Samoa"=>"AS",
"Andorra"=>"AD",
"Angola"=>"AO",
"Anguilla"=>"AI",
"Antarctica"=>"AQ",); ?>
<script>
    $(document).ready(function() {
        var jqueryarray = <?php echo json_encode($myvalues ); ?>;
        for (var i = 0; i < jqueryarray.length; i++) 
		{
            console.log(jqueryarray[i]);
        };
		
		$("#type").change
		(function (){
        var val = $(this).val();
		if (val == "Japan (日本)"=>"JP",) 
		{
            $("#ddlViewBy").html("<select id=\"jap\"><option value=\"9\">Fukuoka </option><option value=\"9\">Hamamatsu</option><option value=\"9\">Himeji</option><option value=\"9\">Hiroshima</option><option value=\"9\">Kagoshima</option><option value=\"9\">Kawasaki</option><option value=\"9\">Kitakyushu</option><option value=\"9\">Kobe</option><option value=\"9\">Kumamoto</option><option value=\"9\">Kyoto</option><option value=\"9\">Matsuyama</option><option value=\"9\">Nagoya</option><option value=\"9\">Niigata</option><option value=\"9\">Osaka</option><option value=\"9\">Sagamihara</option><option value=\"9\">Sapporo</option>	<option value=\"9\">Shizuoka</option><option value=\"9\">Tokyo</option><option value=\"9\">Utsunomiya</option><option value=\"9\">Yokohama</option></select>");
		} 	
		else if (val =="Andorra")
		{
		    $("#ddlViewBy").html("<select id=\"pin\"><option value=\"8\">light</option><option value=\"8\">Saint</option><option value=\"8\">grin</option><option value=\"8\">cake</option></select>");
		
		}
});

</script>
<select id ="type">
<option value="">-----------------</option>
	
<?php
foreach($myvalues as $key => $value):
echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!!
endforeach;
?>
</select>
	<select id="ddlViewBy">
			<option value="">select city</script></option>	
	</select>	
</body>
</html>

使用PHP数组进行javascript选择选项

1 个解决方案

#1


0  

You are trying to put select inside another select.

您正在尝试将select放入另一个select中。

$("#ddlViewBy").html("<select id=\"pin\"><option value=\"8\">light</option><option value=\"8\">Saint</option><option value=\"8\">grin</option><option value=\"8\">cake</option></select>");

Try to put just the options, without tags. That should work for the javascript part.

尝试只选择没有标签的选项。这应该适用于javascript部分。

This is clearly not javascript. I don't know what it is:

这显然不是javascript。我不知道它是什么:

if (val == "Japan (日本)"=>"JP",); // you are trying to compare javascript with PHP style array element.

Javascript is executed in the browser and PHP is executed on the server, they cannot run at the same time on your webpage. PHP is executed before your webpage even gets to the browser via network. That might clear things up a little bit and help you avoid confusing these two.

Javascript在浏览器中执行,PHP在服务器上执行,它们无法在您的网页上同时运行。在您的网页甚至通过网络访问浏览器之前执行PHP。这可能会使事情变得清晰,并帮助您避免混淆这两者。

You can create an map-like object and then do something like:

您可以创建类似地图的对象,然后执行以下操作:

if(val === mapObject['JP']) // or something similar

You can just create an array of strings and then concatenate them into the big HTML if you really need to do it this way (changing innerHTML of the DOM element).

您可以创建一个字符串数组,然后将它们连接到大HTML中,如果您真的需要这样做(更改DOM元素的innerHTML)。

#1


0  

You are trying to put select inside another select.

您正在尝试将select放入另一个select中。

$("#ddlViewBy").html("<select id=\"pin\"><option value=\"8\">light</option><option value=\"8\">Saint</option><option value=\"8\">grin</option><option value=\"8\">cake</option></select>");

Try to put just the options, without tags. That should work for the javascript part.

尝试只选择没有标签的选项。这应该适用于javascript部分。

This is clearly not javascript. I don't know what it is:

这显然不是javascript。我不知道它是什么:

if (val == "Japan (日本)"=>"JP",); // you are trying to compare javascript with PHP style array element.

Javascript is executed in the browser and PHP is executed on the server, they cannot run at the same time on your webpage. PHP is executed before your webpage even gets to the browser via network. That might clear things up a little bit and help you avoid confusing these two.

Javascript在浏览器中执行,PHP在服务器上执行,它们无法在您的网页上同时运行。在您的网页甚至通过网络访问浏览器之前执行PHP。这可能会使事情变得清晰,并帮助您避免混淆这两者。

You can create an map-like object and then do something like:

您可以创建类似地图的对象,然后执行以下操作:

if(val === mapObject['JP']) // or something similar

You can just create an array of strings and then concatenate them into the big HTML if you really need to do it this way (changing innerHTML of the DOM element).

您可以创建一个字符串数组,然后将它们连接到大HTML中,如果您真的需要这样做(更改DOM元素的innerHTML)。