【发布时间】: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