我需要这段代码的帮助。现在,如果表单中有一些变化,它每次都会执行reload。我只想在单击某个单选按钮时将其设置为reload / change。
我试过将onChange或onClick函数添加到收音机中,但没有起作用。该怎么办呢。另外,我需要这些收音机来存储哪个是被选中的。
代码:
<?php
session_start();
$radio = "";
if(isset($_POST['radio'])){
$_SESSION['radio'] = $_POST['radio'];
}
if(isset($_SESSION['radio'])){
$radio = $_SESSION['radio'];
}
?>
<html lang="en">
<head>
<meta charset="utf-8">
<title>html template</title>
<meta name="description" content="html template">
<meta name="author" content="dp">
</head>
<body>
<form class="sellForm" action="" method="post" onchange="this.submit()">
<table>
<input <?php if ($radio=='intern'){ echo 'checked="checked"';} ?> type="radio" id="internSale" name="radio" value="intern" checked><label class="sellRadio" for="internSale">Intern</label>
<input <?php if ($radio=='adaptee'){ echo 'checked="checked"';} ?> type="radio" id="adapteeSale" name="radio" value="adaptee"><label class="sellRadio" for="adapteeSale">Adaptee</label>
<tr <?php if ($radio=='intern'){ echo 'style="display: none;"';}?>><td><label style="font-weight: bold;">Adaptee ID:</label></td><td><input type="number" placeholder="210301001"></td></tr>
<tr <?php if ($radio=='intern'){ echo 'style="display: none;"';}?>><td><label style="font-weight: bold;">Customer:</label></td><td><input type="number" placeholder="730477541"></td></tr>
<tr><td><label style="font-weight: bold;">Item/s:</label></td><td><select name="selectItems" class="positionSelect" id="mySelect">
<option value=""></option>
<option value="1">option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
<option value="4">option4</option>
</select></td></tr>
<tr><td><label style="font-weight: bold;">Price:</label></td><td><input type="float" placeholder="170.00">,-</td></tr>
<tr><td><label style="font-weight: bold;">Note:</label></td><td><input type="text" placeholder="note here"></td></tr>
<tr><td><input type="submit"></td></tr>
</table>
</form>
<?php echo $radio;?>
</body>
</html>发布于 2021-03-02 00:19:37
更改此设置:
<form class="sellForm" action="" method="post" onchange="this.submit()">至:
<form id="sellForm" action="" method="post" >并添加以下js:
function myFunction(){
document.getElementById("sellForm").submit();
}最后,将onclick事件添加到要用作触发器的单选按钮,并检查该按钮是否被选中(应该将其添加到myFunction())
发布于 2021-03-02 00:35:45
您需要将event change listener添加到无线电并删除onchange="this.submit()"
jQuery
$('input[type="radio"]').on('change', function() {
this.parentElement.submit(); // go to <form> then submit
});香草JS
document.querySelectorAll('input[type="radio"]').forEach(function(el) {
el.addEventListener('change', function() {
this.parentElement.submit(); // go to <form> then submit
})
});发布于 2021-03-02 00:36:04
<form name="quiz" id="quizable">
<table id="q_table">
<tr><td>Banana</td><td><input type="radio" name="q1" id="q1_1" /></td></tr>
<tr><td>Apple</td><td><input type="radio" name="q1" id="q1_2" /></td></tr>
<tr><td>Coconut</td><td><input type="radio" name="q1" id="q1_3" /></td></tr>
<tr><td>Cherry</td><td><input type="radio" name="q1" id="q1_4" /></td></tr>
</table>
</form>
https://stackoverflow.com/questions/66425528
复制相似问题