First click on bb link and you will see hello drop down box and click on submit button on next page you will see Hi output , but in fact it should be Hello
when clicked on cc link you will see hi drop down box When submit button is clicked the output still remain the same hi so corrected value is not printed on next page
File Name : enable.php
<html>
<head>
开发者_C百科 <script type="text/javascript">
function showdetail(boxid){
document.getElementById(boxid).style.display = "block";
}
function hidedetail(boxid){
document.getElementById(boxid).style.display = "none";
}
</script>
</head>
<body>
<form name="abc" method="post" action="abc.php">
<ul>
<li><a id="r" href="#" title="" alt="" onclick="showdetail('res'); hidedetail('comm');">bb</a></li>
<li style="width: 1px;">|</li>
<li style="width:85px;"><a id="r" href="#" title="" alt="" onclick="showdetail('comm'); hidedetail('res');">Cc</a></li>
<li style="width: 2px;">|</li>
</ul>`
<div id="res">
<select id="pr" name="pr" class="textarial12 textcolorgrey flot_left">
<option value="Hello">Hello</option>
</select>
</div>
<div id="comm" style="display:none;">
<select id="pr" name="pr" class="textarial12 textcolorgrey flot_left">
<option value="Hi">Hi</option>
</select>
</div>
<input type="submit" value="submit" />
</form>
</body>
</html>
File name : abc.php
<?php
$PrType=$_POST['pr'];
echo $PrType;
?>
Your problem is that both <select>
s are in the DOM, with the same name. When the form submits, it grabs all form element with a name, and since both <select>
s have the same name, the 2nd ('hi') overwrites the 1st ('hello').
To stop the form from submitting a value, make the input disabled
.
Note: You really shouldn't have multiple elements with the same id
. id
s in HTML are supposed to be unique.
精彩评论