【问题标题】:Form event onChange not working on dropdown selection表单事件 onChange 不适用于下拉选择
【发布时间】:2015-01-16 14:27:20
【问题描述】:

我有一个表单可以完美运行并根据单选按钮更改表单的操作:

 <form name="form1" method="post" action="[[base_urlnt]]/electricity-rate-plans">
 <input type="radio" name="energy" value="electricity" checked>
 <label for="electricity">Electricity</label>
 <input type="radio" name="energy" value="gas" onChange="if(this.checked){document.forms[0].action='[[base_urlnt]]/natural-gas-rate-plans'}">
 <label for="gas">Natural Gas</label>
 <input name="service_zip_5" type="text" id="search" placeholder="Enter ZIP Code"><button type="submit" class="bigButton">Get Rates</button>
 </form>

我想做同样的事情,但是通过使用 onChange 而不是在单选按钮上更改另一个表单上的操作,而是使用下面看到的下拉列表。我做错了什么?

<form name="form1" method="post" action="[[base_url]]alberta/electricity-plans">
<p>Choose Your Service</p>
<select name="service" id="service" title="Choose Your Service">
<option value="Electricity" selected="selected">Electricity</option>
<option value="Gas" onChange="if(this.selected){document.forms[0].action='[[base_url]]alberta/natural-gas-plans'}">Natural Gas</option>
<option value="Dual" onChange="if(this.selected){document.forms[0].action='[[base_url]]alberta/dual-fuel-plans'}">Dual Fuel</option>
</select>
<p>Enter Your ZIP Code</p>
<input name="service_zip_5" type="text" id="zipcode">
<button type="submit" id="submit">Get Started</button>
</form>

我试过了 onChange="if(this.checked) 首先是一个想法,既然它是一个选择,它应该是 onChange="if(this.selected) 但都不起作用。这不是一个函数吗?

【问题讨论】:

  • Here 是我对类似问题给出的答案的链接。希望对您有所帮助。

标签: javascript forms


【解决方案1】:

你需要稍微改变一下。

您的 onchange 事件应该绑定到 select 而不是单个选项。

我还在每个选项中添加了一个属性“data-url”来存储需要添加到表单操作 onchange 中的 url。

这个“data-url”属性可以被javascript函数用来更新表单动作。这也意味着您不必在所有 onchange 属性中重复相同的代码。您可以调用一个提取相应“data-url”的函数。

见 - http://jsfiddle.net/williamtdavies/8kxzaquw/

<form name="form1" method="post" action="[[base_url]]alberta/electricity-plans">
    <p>Choose Your Service</p>
    <select name="service" id="service" title="Choose Your Service" onChange="changeFormAction(this);">
        <option value="Electricity" data-url="[[base_url]]alberta/electricity-plans">Electricity</option>
        <option value="Gas" data-url="[[base_url]]alberta/natural-gas-plans">Natural Gas</option>
        <option value="Dual" data-url="[[base_url]]alberta/dual-fuel-plans">Dual Fuel</option>
    </select>
    <p>Enter Your ZIP Code</p>
    <input name="service_zip_5" type="text" id="zipcode"/>
    <button type="submit" id="submit">Get Started</button>
</form>

<script>
    function changeFormAction(elem){
    document.forms[0].action = elem.options[elem.selectedIndex].getAttribute("data-url"); 
    }
</script>

【讨论】:

  • 它仍然一直把我带到默认的电力页面,是因为在表单操作中它仍然有这个并且需要更改为其他内容吗?
  • 我更新了jsfiddle 以包含表单操作的警报,以便您可以在提交表单时查看当前操作。如果您可以使用最新代码设置 jsfiddle,我可能会提供帮助,但目前脱离上下文,这是您需要的。
  • 这是我的 cms 上忽略 JS 的地方,我必须解决这个问题。谢谢!
  • 会不会是页面上有多个表单,并且JS没有直接绑定到这个表单(即使我在表单之后立即有JS)?如果是这样,有没有办法将这个表单绑定到这个js?
  • 可能是这样。如果您查看页面的源代码并在页面上发现多个表单元素。您将需要寻找某种标识符/向要更新的表单添加新标识符。
猜你喜欢
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
相关资源
最近更新 更多