所有必填字段在本地运行良好,但是当我尝试从localhost运行相同页面时 - 使用jQuery创建的字段不检查输入是否已填充。 select中的必需选项在两种情况下都能正常工作。
<html>
<head>
<title>Submit your data</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href="style.css" rel="stylesheet">
<script src="jquery+ajax.js"></script>
</head>
<body>
<div id="mainform">
<h1>Insert your data</h1>
<form method="post" action="add_product.php">
<input type="hidden" name="submitted" value="true" required/>
<fieldset id="product_list">
<legend>New Product</legend>
<label>SKU:<input name="sku" required/></label>
<label>Name:<input name="name" required/></label>
<label>Price:<input name="price" required/></label>
<label>Product Type</label>
<select id="Product_Type" required>
<option value="" selected>Select product type</option>
<option value="Dvd" >DVD</option>
<option value="Book">Book</option>
<option value="Furniture">Furniture</option>
</select>
</fieldset>
</div>
<br />
<input type="submit" id="submit" value="Add"/>
<div id='result'></div>
<button id="back" class="back" onclick="location.href='index.html';">Go Back</button>
</body>
<!-- onClick="window.location.reload()" -->
</html>
使用jQuery创建表单,此处生成的字段不适用于localhost
var $input_DVD = $('<input id="dvd" placeholder="Size in Mb" required/>');
var $input_Book = $('<input id="book" placeholder="Weight in Kg" required/>');
var $input_FurnitureHeight = $('<input id="furnh" placeholder="Height in Cm" required/>');
var $input_FurnitureWidth = $('<input id="furnw" placeholder="Width in Cm" required/>');
var $input_FurnitureLength = $('<input id="furnl" placeholder="Length in Cm" required/>');
$(document).ready(function() {
$('select#Product_Type').on('change', function() {
$('#container').remove();
var val = $(this).val()
$container = $('<fieldset id="container" class="inner" ></fieldset>');
if (val == "Dvd")$input_DVD.appendTo($container);
if (val == "Book") $input_Book.appendTo($container);
if (val == "Furniture"){
$input_FurnitureHeight.appendTo($container);
$input_FurnitureWidth.appendTo($container);
$input_FurnitureLength.appendTo($container);
}
$container.insertAfter($(this)).val();
})
});
答案 0 :(得分:0)
请确保您在本地系统中已连接互联网,因为您没有使用本地jquery库文件,如果显示任何错误,请检查浏览器控制台。
var $input_DVD = $('<input id="dvd" placeholder="Size in Mb" required/>');
var $input_Book = $('<input id="book" placeholder="Weight in Kg" required/>');
var $input_FurnitureHeight = $('<input id="furnh" placeholder="Height in Cm" required/>');
var $input_FurnitureWidth = $('<input id="furnw" placeholder="Width in Cm" required/>');
var $input_FurnitureLength = $('<input id="furnl" placeholder="Length in Cm" required/>');
$(document).ready(function() {
$('select#Product_Type').on('change', function() {
$('#container').remove();
var val = $(this).val()
$container = $('<fieldset id="container" class="inner" ></fieldset>');
if (val == "Dvd")$input_DVD.appendTo($container);
if (val == "Book") $input_Book.appendTo($container);
if (val == "Furniture"){
$input_FurnitureHeight.appendTo($container);
$input_FurnitureWidth.appendTo($container);
$input_FurnitureLength.appendTo($container);
}
$container.insertAfter($(this)).val();
})
});
&#13;
<html>
<head>
<title>Submit your data</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href="style.css" rel="stylesheet">
<script src="jquery+ajax.js"></script>
</head>
<body>
<div id="mainform">
<h1>Insert your data</h1>
<form method="post" action="add_product.php">
<input type="hidden" name="submitted" value="true" required/>
<fieldset id="product_list">
<legend>New Product</legend>
<label>SKU:<input name="sku" required/></label>
<label>Name:<input name="name" required/></label>
<label>Price:<input name="price" required/></label>
<label>Product Type</label>
<select id="Product_Type" required>
<option value="" selected>Select product type</option>
<option value="Dvd" >DVD</option>
<option value="Book">Book</option>
<option value="Furniture">Furniture</option>
</select>
</fieldset>
</div>
<br />
<input type="submit" id="submit" value="Add"/>
<div id='result'></div>
<button id="back" class="back" onclick="location.href='index.html';">Go Back</button>
</body>
<!-- onClick="window.location.reload()" -->
</html>
&#13;