简体   繁体   English

如何链接到表单中的“选择”选项

[英]How can I link to the 'select' option within my form

I have pricing packages for my website.我有我的网站的定价包。

This is how I want the enquiry of my packages to work:这就是我希望查询我的包裹的方式:

  1. I want the user to click on the 'Enquire' button on my packages page eg The bronze package:我希望用户点击我的包裹页面上的“查询”按钮,例如青铜 package:
<div class="card mb-4 box-shadow">
          <div class="card-header">
            <h3 class="my-0 font-weight-normal">Single-Page Website 🥉</h3>
          </div>
          <div class="card-body">
            <h4 class="card-title pricing-card-title">£250 <small class="text-muted">/ once off</small></h4>
            <ul class="list-unstyled mt-3 mb-4">
              <li><b>Fully responsive on mobile and tablet devices</b></li>
              <br>
              <li><b>Page speed optimisation (2-4 seconds load time)</b></li>
              <br>
              <li><b>Uploaded to the internet (fees do not apply)</b></li>
              <br>
              <li><b>On-page SEO provided (Google ranking)</b></li>
            </ul>
            <a class="book-a-call-link" href="/contact.php"><button type="button" class="btn btn-lg btn-block book-a-call-secondary"><b>Book a Call</b></button></a>
          </div>
        </div>
  1. Once the user has clicked on the button.一旦用户点击了按钮。 I want them to be taken to my form on my contact page.我希望他们被带到我的联系页面上的表格中。 Except, I want the link they clicked on to activate a certain select option by default.除了,我希望他们点击的链接默认激活某个 select 选项。

Eg If someone clicks on the 'Enquire' button for the bronze package.例如,如果有人点击青铜 package 的“查询”按钮。 I want them to be taken to /contact.php with the 'Bronze Package' option already selected for them.我希望他们被带到 /contact.php 并且已经为他们选择了“青铜包”选项。

For context here is a small part of the form:对于上下文,这里是表单的一小部分:

<div class="form-group">
        <select class="form-control" id="exampleFormControlSelect1">
          <option>General Enquiry</option>
          <option id="bronze">Bronze Package</option>
          <option>Silver Package</option>
          <option>Gold Package</option>
        </select>
      </div>

Notice that the 'Bronze Package' option has an id of 'bronze'.请注意,“Bronze Package”选项的 id 为“bronze”。 I attempted to link to it with /contact.php/#bronze.我试图用 /contact.php/#bronze 链接到它。 This would work for other elements.这适用于其他元素。 However, not with the select option.但是,不适用于 select 选项。

My three questions are:我的三个问题是:

  1. Can I somehow get this feature to work with the select option?我能否以某种方式使此功能与 select 选项一起使用?
  2. If not, will I need JavaScript to do this?如果没有,我需要 JavaScript 来执行此操作吗?
  3. Wherever I can use HTML or JavaScript, how do I get this process to work the way I want it to?无论我在哪里可以使用 HTML 或 JavaScript,我如何让这个过程按照我想要的方式工作?

I don't see any buttons in the first chunk of code, but it sounds like you want associate an event handler with that button.我在第一段代码中看不到任何按钮,但听起来您希望将事件处理程序与该按钮相关联。 When the event is triggered, you want the user to be redirected to your contact page by setting window.location.href to the url of your contact page.触发事件时,您希望通过将 window.location.href 设置为您的联系页面的 url 来将用户重定向到您的联系页面。 Your contact page, in turn, should accept a query string to select a particular option.反过来,您的联系页面应该接受 select 特定选项的查询字符串。 For example, &package=bronze.例如,&package=青铜。 You want to update your contact page so the select has a name of package, and update the option has a value.您想更新您的联系页面,因此 select 的名称为 package,并更新选项有一个值。 You can, of course, also do this with JavaScript and use the form as is.当然,您也可以使用 JavaScript 执行此操作,并按原样使用表单。

It looks like you are trying to use a "Destination Anchor" (which is intended for deeplink to specific parts of a destination webpage) to avoid having to implement a pre-selected form.您似乎正在尝试使用“目标锚点”(用于深度链接到目标网页的特定部分)以避免必须实施预选表单。 You will need to use javascript or modify your php in order to do this.您需要使用 javascript 或修改您的 php 才能执行此操作。

For either solution, you should modify your links to add a url parameter identifying which package the user selected - eg /contact.php?tier=bronze对于任一解决方案,您应该修改您的链接以添加一个 url 参数来标识用户选择了哪个 package - 例如/contact.php?tier=bronze

In php, you would check the url parameters in $_GET for the variable, and then output a slightly different html page (with the preselected option as the default value). In php, you would check the url parameters in $_GET for the variable, and then output a slightly different html page (with the preselected option as the default value). Use <option selected="selected"> on the proper在正确的地方使用<option selected="selected">

In javascript, you could do something similar - here is a way to check for url parameters in javascript , which you could then add conditional logic to add the selected="selected" parameter to the proper <option>在 javascript 中,您可以执行类似的操作 - 这是一种检查<option> 中的 url 参数的方法,然后您可以添加条件逻辑以将selected="selected"参数添加到

Finally, I'd like to get a royalty from each of the payment options you are offering as compensation for my assistance.最后,我想从您提供的每种付款方式中获得版税,作为对我的帮助的补偿。 Thanks in advance.提前致谢。

If select is static then use <option id="bronze" selected>Bronze Package</option>如果select是 static 然后使用<option id="bronze" selected>Bronze Package</option>

<select class="form-control" id="exampleFormControlSelect1">
      <option>General Enquiry</option>
      <option id="bronze" selected>Bronze Package</option>
      <option>Silver Package</option>
      <option>Gold Package</option>
    </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM