0

SO上的某个人帮助我为一个选择框编写了这个脚本,它将根据下拉列表中的选择显示隐藏的行

function showNext(opt)  {
    takeAway()
    var optMap={
        "Picked Up":"#pickedup",
        "Bus to alternate":"#altad2,#altad1",
        "Walk to alternate":"#altad2,#altad1",
    }
    $(optMap[opt]).css("display","") ;
}

function takeAway(){
    $("#pickedup,#altad2,#altad1").css("display", "none");
}

我需要一些可以运行 onload 的东西,所以如果已经选择了某些东西,行会显示

这是将受脚本影响的选择

<tr class="~[evenoddrow]"> 
            <td class="bold">In the event of early dismissal I would like my child to:</td><td id="Emergclose_Action">~([01]Emergclose_Action)</td><td class="gwCen"></td>
            <td><select name="eReg|Emergclose_Action" id="eReg|Emergclose_Action" onChange="showNext(value);">
                    <option></option><option value="Walk home as normal">Walk home as normal</option><option value="Ride the bus as normal">Ride the bus as normal</option>
<option value="Picked Up">Be picked up immediately after dismissal</option><option value="Bus to alternate">Ride the bus to an alternate address</option><option value="Walk to alternate">Walk to an alternate address</option>
                </select>           
            </td>
        </tr>

我尝试了以下,它没有工作..

function showNext(opt)  {
        takeAway()

      var optMap={
             "Picked Up":"#pickedup",
             "Bus to alternate":"#altad2,#altad1",
             "Walk to alternate":"#altad2,#altad1",
              }
           $(optMap[opt]).css("display","") ;       

}
function takeAway(){
    $("#pickedup,#altad2,#altad1").css("display", "none");
 }
$(function(){
   $('#eReg|Emergclose_Action').trigger('onchange');
});
4

2 回答 2

0

尝试onChange="showNext(value);"从您的选择元素中删除并更改 ID。在选择器中使用管道|不是一个好习惯,需要进行一轮工作......没有它会更容易。将其替换为下划线_

所以你的选择 html 是:

<select name="eReg|Emergclose_Action" id="eReg_Emergclose_Action">

然后以这种方式执行javascript:

$(function(){

   //anything in here will run after the page loads

   //assign showNext to the change event
   $('#eReg_Emergclose_Action').change(function(){
      showNext($(this).val());
   }).change();//manually trigger the change event after its attached

});
于 2013-02-21T19:20:54.563 回答
0

如果有人之前已经选择了“是”,则必须在页面加载时运行的脚本可以使用 Jquery 进行如下优化

$('#eReg|Emergclose_Action').trigger('onchange');

于 2013-02-21T19:37:37.587 回答