默认情况下选中单选按钮但在提交时未选中

时间:2013-04-22 08:38:30

标签: php javascript html

我有一组单选按钮,如下所示。我打算在初始加载页面时检查默认的单选按钮是塑料。下面的代码只保留我选择的单选按钮。

<div id="material">
<input type="radio"  name="materials" value="glass" style="vertical-align: middle"
  onchange="this.form.submit()"
  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'glass') {echo "checked";}} ?>
  />
<label for="material">Glass</label> <br/>

<input type="radio"  name="materials"  style="vertical-align: middle" value="plastic"
  onchange="this.form.submit()"
  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'plastic') {echo "checked";}} ?>
/>
<label for="material" >Plastic (Organic)</label>
</div>

3 个答案:

答案 0 :(得分:1)

从单选按钮中删除onchange="this.form.submit()",然后在页眉中的脚本标记之间添加:

<script>
window.onload = function() {
    // when the form is submitted
    document.forms[0].onsubmit = function() {
        // get all radio buttons with name = materials
        var radios = document.getElementsByName('materials');
        // for each radio button
        for(i=0; i<radios.length; i++) {
            // uncheck radio buttons
            radios[i].checked = false;
        }
    };
};
</script>

答案 1 :(得分:0)

尝试将“已检查”更改为“已检查=”已选中“'

答案 2 :(得分:0)

尝试下面给出的代码,它有所需的更改

<div id="material">
    <input type="radio"  name="materials" value="glass" style="vertical-align: middle"  onchange="this.form.submit()" <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'glass') {echo "checked";}} ?>/>
     <label for="material">Glass</label> <br/>

    <input type="radio" checked="checked"   name="materials"  style="vertical-align: middle"  value="plastic"  onchange="this.form.submit()"  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'plastic') {echo "checked";}} ?>/>
    <label for="material" >Plastic (Organic)</label>
    </div>

已更新

<form method="post" >
 <div id="material">
 <?php if(isset($_POST['materials'])) {
    if($_POST['materials'] == 'glass'){
    ?>
<input type="radio" checked="checked" name="materials" value="glass" style="vertical-align: middle"  onchange="this.form.submit()" <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'glass') {echo "checked";}} ?>/>
 <label for="material">Glass</label> <br/>
 <input type="radio"   name="materials"  style="vertical-align: middle"  value="plastic"  onchange="this.form.submit()"  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'plastic') {echo "checked";}} ?>/>
<label for="material" >Plastic (Organic)</label>
 <?php } 
 else { ?>
  <input type="radio"  name="materials" value="glass" style="vertical-align: middle"  onchange="this.form.submit()" <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'glass') {echo "checked";}} ?>/>
  <label for="material">Glass</label> <br/>
 <input type="radio" checked="checked"  name="materials"  style="vertical-align: middle"  value="plastic"  onchange="this.form.submit()"  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'plastic') {echo "checked";}} ?>/>
 <label for="material" >Plastic (Organic)</label>
 <?php } 
 } else { ?>
 <input type="radio"  name="materials" value="glass" style="vertical-align: middle"  onchange="this.form.submit()" <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'glass') {echo "checked";}} ?>/>
 <label for="material">Glass</label> <br/>
<input type="radio" checked="checked"  name="materials"  style="vertical-align: middle"  value="plastic"  onchange="this.form.submit()"  <?php if(isset($_POST['materials'])){ if($_POST['materials'] == 'plastic') {echo "checked";}} ?>/>
<label for="material" >Plastic (Organic)</label>
<?php } ?>
</div>

<input type="submit" name="submit">
</form>