请原谅这位新手用户!我正在尝试创建一个表,用户可以从一个音乐会节目列表中进行选择,然后为所选节目选择一个日期(从2个或3个相关日期中选择)。同一组下拉菜单必须再重复3次。这是一个部分季节订阅套餐,其中顾客可以选择6个节目中的4个和每个节目的期望日期,并且所有这些信息需要发布到Paypal进行处理/购买。
请参阅此测试页:http://early-music.org/Test_forTzN.html和此jsfiddle.net页面:jsfiddle.net/saraswati/v6Pur/31
正如人们在网页上看到的那样,我的剧本适用于“Concert 1”,但不适用于Concert 2,3和4.在jsfiddle页面上,即使Concert 1也不起作用。
感谢您的帮助!
以下是代码:
<script type="text/javascript">
var progamsAndDates = {};
progamsAndDates['TUDORS'] = ['Sept. 15', 'Sept. 16'];
progamsAndDates['NOCHES'] = ['Oct. 20', 'Oct. 21'];
progamsAndDates['CHRISTMAS'] = ['Dec. 14', 'Dec. 15', 'Dec. 16'];
progamsAndDates['CELTIC'] = ['Jan. 26', 'Jan. 27'];
progamsAndDates['UNREQUITED'] = ['Mar. 02', 'Mar. 03'];
progamsAndDates['SECRET'] = ['Apr. 20', 'Apr. 21'];
function ChangeDateDropList (id) {
var programDropList = document.getElementById ("program"+id);
var dateDropList = document.getElementById ("date"+id);
var selProgram = programDropList.options[programDropList.selectedIndex].value;
// remove all dates
while (dateDropList.options.length) {
dateDropList.remove (0);
}
// add new dates
var dates = progamsAndDates[selProgram];
if (dates) {
for (var i=0; i < dates.length; i++) {
var date = new Option (dates[i], i);
dateDropList.options.add (date);
}
}
}
</script>
<tr id=row2>
<td><input type="hidden" name="os0" value="Concert 1"><body onload="ChangeDateDropList ();">
<select id="program" onchange="ChangeDateDropList();">
<option value="" selected="selected">Select a Program</option>
<option value="TUDORS">The Tudors</option>
<option value="NOCHES">Noches, Noches</option>
<option value="CHRISTMAS">Christmas Eurotour</option>
<option value="CELTIC">Celtic Trinity</option>
<option value="UNREQUITED">Unrequited Love</option>
<option value="SECRET">Secret No More</option>
</select>
<select id="date">
</select>
</body>
</td>
<td><input type="hidden" name="os1" value="Concert 2"><body onload="ChangeDateDropList ();">
<select id="program1" onchange="ChangeDateDropList(1);">
<option value="" selected="selected">Select a Program</option>
<option value="TUDORS">The Tudors</option>
<option value="NOCHES">Noches, Noches</option>
<option value="CHRISTMAS">Christmas Eurotour</option>
<option value="CELTIC">Celtic Trinity</option>
<option value="UNREQUITED">Unrequited Love</option>
<option value="SECRET">Secret No More</option>
</select>
<select id="date1">
</select>
</body>
</td>
<td><input type="hidden" name="os2" value="Concert 3"><body onload="ChangeDateDropList ();">
<select id="program2" onchange="ChangeDateDropList(2);">
<option value="" selected="selected">Select a Program</option>
<option value="TUDORS">The Tudors</option>
<option value="NOCHES">Noches, Noches</option>
<option value="CHRISTMAS">Christmas Eurotour</option>
<option value="CELTIC">Celtic Trinity</option>
<option value="UNREQUITED">Unrequited Love</option>
<option value="SECRET">Secret No More</option>
</select>
<select id="date2">
</select>
</body>
</td>
<td><input type="hidden" name="os3" value="Concert 4"><body onload="ChangeDateDropList ();">
<select id="program3" onchange="ChangeDateDropList(3);">
<option value="" selected="selected">Select a Program</option>
<option value="TUDORS">The Tudors</option>
<option value="NOCHES">Noches, Noches</option>
<option value="CHRISTMAS">Christmas Eurotour</option>
<option value="CELTIC">Celtic Trinity</option>
<option value="UNREQUITED">Unrequited Love</option>
<option value="SECRET">Secret No More</option>
</select>
<select id="date3">
</select>
</body>
</td>
</tr>
</table>
<br>
<center>
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</center>
答案 0 :(得分:0)
您有几个问题:
您有多个“程序”和“日期”元素。其中每个都需要自己的唯一ID,以便您知道在javascript中引用的是哪一个。
您的ChangeDateDropList函数需要1个参数,而您没有传递任何参数。 (注意:这不是不正确的语法。只是指出它,因为它出现了正确的路由需要将参数传递给你的函数,但忘记在函数调用中实际传递参数)。
解决此问题的最简单方法是使用数字后缀重命名每个程序和日期元素,例如:“program1”,“date1”,“program2”,“date2”等。一旦完成,您可以传递数字添加到ChangeDateDropList函数的后缀,然后使用它来正确引用元素:
function ChangeDateDropList (id) {
var programDropList = document.getElementById ("program"+id);
var dateDropList = document.getElementById ("date"+id);
...
不要忘记更新你的onchange电话:
<select id="program1" onchange="ChangeDateDropList(1);">