I want a javascript that hide some fields the first time when I go to my index.php. After I made a selection, some fields show up (depends on selection). But on that index.php, There is a form. The moment I press 'submit', I want to stay on the same page, but the fields must stay as they are..
But now all my fields hide (as on first page load)?
My code:
<script type="text/javascript">
function firstLoad()
{
<!-- HIDE FIELDS ON DOC START -->
$(".diameterRound").hide();
$(".lengthSquare").hide();
$(".lengthRectangle").hide();
$(".material").hide();
$(".thickness").hide();
$(".calculate").hide();
}
window.onload=firstLoad;
</script>
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
.... Can somebody help me? I don't know much about javascript..
Edit, my script and form:
<script type="text/javascript">
function firstLoad()
{
<!-- HIDE FIELDS ON DOC START -->
$(".diameterRound").hide();
$(".lengthSquare").hide();
$(".lengthRectangle").hide();
$(".material").hide();
$(".thickness").hide();
$(".calculate").hide();
}
$(document).ready(function()
{
<!-- GET SELECTED SHAPE -->
$selectedshape=$("input:radio[name='shape']:checked").val();
$('input[type="radio"]').click(function()
{
<!-- ROUND -->
if($(this).attr("value")=="round")
{
$(".diameterRound").show();
$(".material").show();
$(".thickness").show();
$(".calculate").show();
$(".lengthSquare").hide();
$(".lengthRectangle").hide();
<!-- images shape selection goed zetten -->
document.getElementById('roundShape').src = "css/images/roundSelect.jpg";
document.getElementById('oblongShape').src = "css/images/oblong.jpg";
document.getElementById('rectShape').src = "css/images/rect.jpg"
document.getElementById('squareShape').src = "css/images/square.jpg";
}
<!-- SQUARE -->
if($(this).attr("value")=="square")
{
$(".lengthSquare").show();
$(".material").show();
$(".thickness").show();
$(".calculate").show();
$(".diameterRound").hide();
$(".lengthRectangle").hide();
<!-- images shape selection goed zetten -->
document.getElementById('roundShape').src = "css/images/round.jpg";
document.getElementById('oblongShape').src = "css/images/oblong.jpg";
document.getElementById('rectShape').src = "css/images/rect.jpg";
document.getElementById('squareShape').src = "css/images/squareSelect.jpg";
}
<!-- RECTANGLE -->
if($(this).attr("value")=="rectangle")
{
$(".lengthRectangle").show();
$(".material").show();
$(".thickness").show();
$(".calculate").show();
$(".lengthSquare").hide();
$(".diameterRound").hide();
<!-- images shape selection goed zetten -->
document.getElementById('roundShape').src = "css/images/round.jpg";
document.getElementById('squareShape').src = "css/images/square.jpg";
document.getElementById('oblongShape').src = "css/images/oblong.jpg";
document.getElementById('rectShape').src = "css/images/rectSelect.jpg";
}
<!-- OBLONG -->
if($(this).attr("value")=="oblong")
{
$(".lengthRectangle").show();
$(".lengthSquare").hide();
$(".diameterRound").hide();
<!-- images shape selection goed zetten -->
document.getElementById('roundShape').src = "css/images/round.jpg";
document.getElementById('squareShape').src = "css/images/square.jpg";
document.getElementById('rectShape').src = "css/images/rect.jpg";
document.getElementById('oblongShape').src = "css/images/oblongSelect.jpg";
}
});
});
window.onload=firstLoad;
</script>
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
<label>
<h2>Step 1: Select your shape</h2>
</label>
<!-- SHAPE SELECTION -->
<div class="row 50%">
<div class="3u 12u(mobilep)">
<label>
<input type="radio" name="shape" value="round" />
<img id="roundShape" src="css/images/round.jpg">
</label>
</div>
<div class="3u 12u(mobilep)">
<label>
<input type="radio" name="shape" value="square" />
<img id="squareShape" src="css/images/square.jpg">
</label>
</div>
<div class="3u 12u(mobilep)">
<label>
<input type="radio" name="shape" value="rectangle" />
<img id="rectShape" src="css/images/rect.jpg">
</label>
</div>
<div class="3u 12u(mobilep)">
<label>
<input type="radio" name="shape" value="oblong" />
<img id="oblongShape" src="css/images/oblong.jpg">
</label>
</div>
</div>
<!-- ROUND - DIAMETER -->
<div class="row 50% diameterRound">
<div class="12u diameterRound">
<input type="text" name="diameterRound" class="diameterRound" id="diameterRound" placeholder="Diameter (mm)" />
</div>
</div>
<!-- SQUARE - LENGTH -->
<div class="row 50% lengthSquare">
<div class="12u lengthSquare">
<input type="text" name="lengthSquare" class="lengthSquare" id="lengthSquare" placeholder="Length (mm)" />
</div>
</div>
<!-- RECTANGLE OBLONG - LENGTH WIDTH -->
<div class="row 50% lengthRectangle">
<div class="6u lengthRectangle">
<input type="text" name="lengthRectangle" class="lengthRectangle" id="lengthRectangle" placeholder="Length (mm)" />
</div>
<div class="6u">
<input type="text" name="widthRectangle" class="widthRectangle" id="widthRectangle" placeholder="Width (mm)" />
</div>
</div>
<div class="row 50% material">
<div class="12u material">
<select class="material" name="material" id="material" >
<option value="" disabled selected>Select your material</option>
<OPTION value="448.38">Mild steel = 45 kg/mm2</OPTION>
<OPTION value="797.12">Stainless steel A4316 = 80 kg/mm2</OPTION>
<OPTION value="523.11">Stainless steel 304 = 52.5 kg/mm2</OPTION>
<OPTION value="199.28">Aluminium = 20 kg/mm2</OPTION>
<OPTION value="1394.96">Hardox 450 = 140 kg/mm2</OPTION>
<OPTION value="1245.50">Raex 400 = 125 kg/mm2</OPTION>
<OPTION value="348.74">Copper = 35 kg/mm2</OPTION>
<OPTION value="548.02">50D Steel = 55 kg/mm2</OPTION>
<OPTION value="617.77">Domex 420 = 62 kg/mm2</OPTION>
<OPTION value="637.69">S355 structual steel = 64 kg/mm2</OPTION>
</select>
</div>
</div>
<div class="row 50% thickness">
<div class="12u thickness">
<input type="text" name="thickness" class ="thickness" id="thickness" placeholder="Thickness of the material (mm)" />
</div>
</div>
<div class="row 50% calculate">
<div class="12u calculate">
<label>
<ul class="actions calculate">
<li><input id="calculate" type="submit" class="button alt" value="Calculate" /></li>
</ul>
</label>
</div>
</div>
</form>
默认情况下,您可以通过将其CSS设置为display来隐藏它们:无
here you submit page without ajax so when you submit form it will reload page and execute again . here firstLoad() function call again and it will hide elements.
Solution
either use ajax for form submit . ajax will not reload page so your element look same as selection
OR
put some conditions on hidden element base on post data on submit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.