【发布时间】:2019-02-10 01:46:17
【问题描述】:
我想从我的联系表单生成的 URL 中解析参数值(GET 方法并强制使用 JQuery 而不是纯 JS)。它在控制台内工作,值被正确读取。我有两个问题来自于不精通 Jquery 和 JS。
我找不到将我拥有的值放入网页(例如,放入表格)的可靠方法。我想要一个表格,以便用户可以看到他们在联系表单中输入的内容。
我的表单中有复选框,这导致从 URL 读取值时出现问题。如果我选中四个框中的两个,则只会读取其中一个并将其打印到控制台中。我需要阅读用户提供的所有信息。我想我需要数组,但在这种情况下如何使用它们?
生成的 URL 示例。
localhost:63342/2018-11-13-html/form_sent.html?phone=4325325235&adress=testadress&order=book1&order=book2&deliverydate=datadostawy&deliverymethod=chinamail&agree=on
我当前用于读取和记录 URL 参数的代码:
<section>
<script>
$.urlParam = function (name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)')
.exec(window.location.href);
if (results == null) {
return 0;
}
return results[1] || 0;
};
console.log($.urlParam('phone'));
console.log($.urlParam('adress'));
console.log($.urlParam('order'));
console.log($.urlParam('deliverydate'));
console.log($.urlParam('deliverymethod'));
</script>
</section>
还有我的表格:
<section>
<header><h2>Contact</h2></header>
<form style="width:500px" action="form_sent.html" method="get">
<fieldset>
<legend>Contact form:</legend>
<fieldset>
<legend>Contact info:</legend>
<span class="label" style="width: 50px">Phone: </span>
<input name="phone" type="tel" required="true"/><br/>
<span class="label" style="width: 50px">Adress: </span>
<input name="adress" type="text" required="true"/><br/>
</fieldset>
<fieldset>
<legend>Order:</legend>
<span class="label" style="width: 100px">Books:</span>
<br>
<input type="checkbox" name="order" value="book1"/>Book1<br/>
<input type="checkbox" name="order" value="book2"/>Book2<br/>
<input type="checkbox" name="order" value="book3"/>Book3<br/>
<input type="checkbox" name="order" value="book4"/>Book4<br/>
</fieldset>
<fieldset>
<legend>Delivery date:</legend>
<span class="label" style="width: 50px">Date: </span>
<input type="text" name="deliverydate" required="true"/><br/>
</fieldset>
<fieldset>
<legend>Delivery method:</legend>
<input type="radio" name="deliverymethod" value="fedx" />FEDx
<input type="radio" name="deliverymethod" value="chinamail"/>China Mail
<input type="radio" name="deliverymethod" value="personal" checked="true"/>In person<br/>
</fieldset>
<input name="agree" type="checkbox" required="true"/> I agree to contact terms<br/>
<input type="reset" value="Reset form"/><input type="submit" value="Send form"/>
</fieldset>
</form>
</section>
【问题讨论】:
标签: javascript jquery html url checkbox