[英]Change select option based on <a> clicked
I've seen it done before, but basically what I'm trying to do is when a user clicks on an option "Missionary Farewell/Homecoming", it redirects them to a contact form with a dropdown menu where "Missionary Farewell/Homecoming" is already selected for them in the contact form, and do that for the various different <a>
tags and different select options. 我以前看过它,但是基本上我要做的是,当用户单击选项“ Missionary Farewell / Homecoming”时,它会将他们重定向到带有下拉菜单的联系表,其中“ Missionary Farewell / Homecoming”已在联系表单中为他们选择,并为各种不同的
<a>
标记和不同的选择选项执行此操作。
HTML Form I'm redirecting to 我重定向到的HTML表单
<form class="contactform" method="post" action="php/events.php">
<input class="leftbox halfbox" name="contactname" placeholder="Contact's Name">
<input class="rightbox halfbox" name="eventname" placeholder="Event Name">
<input class="leftbox halfbox" name="contactphone" placeholder="Contact Phone">
<input class="rightbox halfbox" name="contactemail" placeholder="Contact Email">
<select class="fullbox stubborn" name="eventtype">
<option>What type of event is it?</option>
<option value="missionary">Missionary Farewell/Homecoming</option>
<option value="highlight">Athletic/Dance Featurette</option>
<option value="birthday">Birthday</option>
<option value="funeral">Funeral</option>
<option value="graduation">Graduation</option>
<option value="anniversary">Anniversary</option>
<option value="engagement">Engagement</option>
<option value="other">Other</option>
</select>
<textarea class="fullbox" id="textarea" name="otheroption" placeholder="If other, please specify"></textarea>
<input class="leftbox halfbox" name="eventloc" placeholder="Event Location">
<input class="rightbox halfbox" name="eventtime" placeholder="Event Duration">
<textarea class="fullbox" id="textarea" name="otherquestions" placeholder="Do you have any other questions/specifications?"></textarea>
<input class="fullbox stubborn" id="submit" name="submit" type="submit" value="Submit">
</form>
Set the anchor's href to be a URL with a query string that carries the event type: 将锚点的href设置为带有带有事件类型的查询字符串的URL:
<a href="form-page.php?eventtype=missionary">...</a>
On the form page form-page.php
a Javascript reads the URL, parses the query string for the eventtype
parameter and sets the option menu accordingly. 在表单页面
form-page.php
,Javascript读取URL,解析eventtype
参数的查询字符串,并相应地设置选项菜单。
In order to do that is better to give an id
to the event dropdown, let's use eventtype
: 为了更好地为事件下拉列表提供
id
,让我们使用eventtype
:
<select class="fullbox stubborn" name="eventtype" id="eventtype">
Then at the end of the body
before the closing tag </body>
you put something like this: 然后在结束标记
</body>
之前的body
末尾,您应放置以下内容:
<script>
function getParameterByName(name)
{
var url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
document.getElementById('eventtype').value = getParameterByName('eventtype');
</script>
This approach is pure client-side (pure html) without php or any server side code to manage the page. 这种方法是纯客户端(纯html),没有php或任何服务器端代码来管理页面。
(Of course the same goal can be accomplished without Javascript server-side-building the form page with the option pre-selected) (当然,无需使用Javascript服务器端构建带有预选选项的表单页面,就可以实现相同的目标)
Credits: 学分:
The function getParameterByName()
comes from: How can I get query string values in JavaScript? 函数
getParameterByName()
来自: 如何获取JavaScript中的查询字符串值?
How to change the selected option by value using JavaScript is described here: HTML SELECT - Change selected option by VALUE using JavaScript 此处介绍了如何使用JavaScript通过值更改所选选项 : HTML SELECT-使用JavaScript通过VALUE更改所选选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.