mia*_*aby 5 html javascript forms jquery
所以我创建了一个表单.在提交该表单后,我创建了一个确认选项卡,以便用户可以确认他的输入.一旦用户确认输入,表格将被提交.
所以我添加了jquery来显示确认选项卡,但是当我点击按钮显示确认选项卡时,表单只是清除并且没有看到确认选项卡...请在下面找到代码
HTML
<div class="tab-content">
<fieldset class="tab-pane active" id="form_tab">
<form id="poform" method="GET">
<table>
<tr>
<th colspan="2"><div class="header_3">Pre-Order Form</div></th>
</tr>
<tr>
<td><label>Account Number:</label></td>
<td><input type="text" name="accountnumber" id="accountnumber" value="" required/></td>
</tr>
<tr>
<td><label>Trade:</label></td>
<span class="text_11">
<td><input type="radio" id="Buy" name="tradetype" class="tradetype" required value="Buy"/> Buy
<input type="radio" id="Sell" name="tradetype" class="tradetype" value="Sell"/> Sell </span></td>
</tr>
<tr>
<td><label>Metal:</label></td>
<span class="text_11">
<td><input type="radio" id="Steel" name="metal" class="metal" required value="Steel"/> Steel
<input type="radio" id="Iron" name="metal" class="metal" value="Iron"/> Iron </span></td>
</tr>
<tr>
<td class="select"><label>Amount:</label></td>
<td><select id="amount" name="amount">
<option value="">Select</option>
<?php include_once "selectamount.php"?></td>
</select>
</tr>
<tr>
<td class="select"><label>Date:</label></td>
<td><select id="date" name="date" id="date">
<option value="">Select</option>
<?php include_once "selectdate.php"?></td>
</select>
</tr>
<tr>
<td colspan="2" align="center">
<button type="button" id="submit_btn">
<span class="chngetext">Check</span>
</button>
</td>
</tr>
</table>
</form>
</fieldset>
<fieldset class="tab-pane" id="conf_tab">
<table>
<tr style="text-align:center">
<th colspan="2"><div class="header_3">Pre-Order Ticket Details</div></th>
</tr>
<tr style="text-align:left">
<td><label>Account Number:</label></td>
<td><mgin><span id="confirm_accountnumber"></span></mgin></td>
</tr>
<tr style="text-align:left">
<td><label>Trade Pre-Order:</label></td>
<td><mgin><span id="confirm_tradetype"></span></mgin></td>
</tr>
<tr>
<td><label>Amount:</label></td>
<td><mgin><span id="confirm_amount"></span> 9999 Pooled Allocated <span id="confirm_metal"></span> Loco Singapore</mgin></td>
</tr>
<tr>
<td><label>On date:</label></td>
<td><mgin><span id="confirm_date"></span></mgin></td>
</tr>
<td><label>Pre-Order Discount:</label></td>
<td><mgin><span id="confirm_data"></span></mgin></td>
</tr>
<tr>
<td><label>Pre-Order Deposit:</label></td>
<td><mgin>SGD 5000.00</mgin></td>
</tr>
<tr>
<td><label>Additional follow up order:</label></td>
<td><mgin>New Sell Order to be given 2 days before purchase order date.</mgin></td>
</tr>
<tr>
<td colspan="2" align="center"><button type="submit" class="btn btn-default" id="confirm_btn">Confirm Order</button></td>
</tr>
</table>
</fieldset>
</div>
Run Code Online (Sandbox Code Playgroud)
jQuery的
$(document).ready(function() {
$('#submit_btn').click(function() {
// Get the value of the field with 'firstname' id.
var accountnumber = $('#accountnumber').val();
var tradetype = $('input.tradetype:checked').val();
var amount = $('#amount').val();
var metal = $('input.metal:checked').val();
var date = $('#date').val();
var data = "<?php include 'retrievepremordisc.php'; ?>";
$('#confirm_accountnumber').text(accountnumber);
$('#confirm_tradetype').text(tradetype);
$('#confirm_amount').text(amount);
$('#confirm_metal').text(metal);
$('#confirm_date').text(date);
$('#confirm_data').text(data);
// Hide form tab and show confirmation tab
$('#form_tab').removeClass('active');
$('#conf_tab').addClass('active');
});
});
Run Code Online (Sandbox Code Playgroud)
PHP文件(retrievepremordisc.php)
<?php
include_once "connect.php";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$form=$_GET;
$trade=$form['tradetype'];
$metal=$form['metal'];
$amount=$form['amount'];
$date=$form['date'];
$stmt = $conn->query("SELECT Discount FROM Contracts WHERE Trade='$trade' AND Metal='$metal' AND Amount='$amount' AND ExpiryDate='$date'");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['Discount'];
}
?>
Run Code Online (Sandbox Code Playgroud)
谢谢!
当您单击提交按钮时,表单将执行提交操作,因此 jQuery 代码不会执行。
改变
<button type="submit" name="submit" id="checkbut">Check Premium</button>
到
<button type="button" name="submit" id="checkbut">Check Premium</button>。
其他方法是使用event.preventDefault();.
$(document).ready(function() {
$('#checkbut').click(function(e) {
e.preventDefault(); // This prevents the action of sending the form.
// Get the value of the field with 'firstname' id.
var accountnumber = $('#accountnumber').val();
var tradetype = $('#tradetype').val();
var amount = $('#amount').val();
var metal = $('#metal').val();
var date = $('#date').val();
$('#confirm_accountnumber').text(accountnumber);
$('#confirm_tradetype').text(tradetype);
$('#confirm_amount').text(amount);
$('#confirm_metal').text(metal);
$('#confirm_date').text(date);
// Hide form tab and show confirmation tab
$('#form_tab').removeClass('active');
$('#conf_tab').addClass('active');
});
});
Run Code Online (Sandbox Code Playgroud)
然而:
ID 选择器在页面中必须是唯一的。你有两个具有相同 id 的按钮。您需要使用唯一标识符正确重命名。
根据您的 html 上下文:
<div id="Sellbut" class="toHide" style="display:none"><button type="submit" name="submit" id="checkbut">Check Premium</button></div>
<div id="Buybut" class="toHide" style="display:none"><button type="submit" name="submit" id="checkbut">Check Discount</button></div>
Run Code Online (Sandbox Code Playgroud)
这些按钮似乎执行不同的操作,因此您需要对其进行重命名。
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |