0

此 javascript 函数参数在脚本运行时没有正确的值。如果我添加一条警告语句来打印参数值,它看起来正确并且脚本运行正常。这有什么可以解释的?

#添加

<form name="add_event_form" id="add_event_form"><input id="event_id" name="event_id" style="visibility: hidden;"/><input id="event_title" name="event_title" style="position: absolute; top: 25px; left: 0px; margin-left: 49px; width: 432px;" onclick="clear_field_color(this);"/><label id="event_title_label" for="event_title" style="position: absolute; top: 30px; left: 0px; margin-left: 10px; ">Title</label><fieldset style="position: absolute; top: 55px; width:276px; padding:3px 5px 10px 5px;"><legend>Event Source</legend><input type="radio" id="select_LiveMass" name="source_select" value="LiveMass" CHECKED onClick="set_selected(\'LiveMass\');" style="margin-right: 5px;"/>LiveMass<br/><span id="channel_span" style="margin-left:20px; margin-right:5px;">Channel</span><select id="channel_list_id" name="channel_list" onfocus="set_radio_button(\'LiveMass\');" onclick="clear_field_color(this);" style="width: 200px;"><option value=""></option></select><br/><input type="radio" id="select_Other" name="source_select" value="Other" onClick="set_selected(\'Other\');" style="margin-right: 5px;"/>Other<br/><span id="event_site_span" style="margin-left:20px; margin-right:19px;">Name</span><input id="event_site" name="event_site" onfocus="set_radio_button(\'Other\');"  onclick="clear_field_color(this);" style="width:196px;"/><br/><span id="event_url_span" style="margin-left:20px; margin-right:37px;">Url</span><input id="event_url" name="event_url" onfocus="set_radio_button(\'Other\');"  onclick="clear_field_color(this);" onchange="validate_url();" style="width:196px;"/></fieldset><fieldset style="position: absolute; top: 55px; margin-left: 299px; width:170px; height: 119px; padding:3px 0px 10px 5px;"><legend>Event Schedule</legend>Start date<input id="start_date" name="start_date" class="date_select" onclick="clear_field_color(this);" style="width:89px; margin-left: 10px; margin-right: 0px;"/><br/>Start time <div style="display:inline;"><input id="start_time" name="start_time" style="width: 40px; margin-left: 6px; padding-left:0px;"/><select id="start_ampm" name="start_time_ampm"><option value="AM">AM</option><option value="PM">PM</option></select></div><br/>End date<input id="end_date" name="end_date" class="date_select" style="width: 89px; margin-left: 15px; margin-right: 0px;"/><br/>End time <div style="display:inline;"><input id="end_time" name="end_time" style="width: 40px; margin-left:11px; padding-left:0px;"/><select id="end_ampm" name="end_time_ampm"><option value="AM">AM</option><option value="PM">PM</option></select></div><div id="sched_buttons"><button href="#" id="event_remind" onclick="show_event_remind(); return false;" style="width: 80px;">Reminders</button>&nbsp;&nbsp;<button href="#" id="recur" onclick="show_event_recur(); return false;" style="width: 80px;">Recurrence</button></div></fieldset><fieldset id="notes" style="position: absolute; top: 192px; width:462px; padding:3px 5px 10px 5px;"><legend>Event Notes</legend><textarea id="event_notes" name="event_notes" style="width: 449px; height:110px; margin-left: 3px;"></textarea></fieldset><div id="event_buttons" style="position: absolute; top: 340px; right: 170px; width:auto;"><button href="#" id="save" onclick="save_event(0); return false;" style="width: 70px;">Save</button>&nbsp;&nbsp;&nbsp;&nbsp;<button href="#" id="cancel" onclick="cancel_event(); return false;" style="width: 70px;">Cancel</button></div></form>

####

function set_radio_button(selection) {
    /* alert ('radio button selection '); */
    switch (selection) {
        case 'LiveMass':
            document.add_event_form.source_select[0].checked = true;
            break;

        case 'Other':
            document.add_event_form.source_select[1].checked = true;
            break;
    }
    set_selected(selection);
}

function set_selected(selection) {
    switch (selection) {
        case 'LiveMass':
            $('#event_site_span').prop("disabled", true);
            $('#event_site').prop("disabled", true);
            $('#event_url_span').prop("disabled", true);
            $('#event_url').prop("disabled", true);
            $('#channel_span').prop("disabled", false);
            $('#channel_list_id').prop("disabled", false);
            break;

        case 'Other':
            $('#channel_span').prop("disabled", true);
            $('#channel_list_id').prop("disabled", true);
            $('#event_site_span').prop("disabled", false);
            $('#event_site').prop("disabled", false);
            $('#event_url_span').prop("disabled", false);
            $('#event_url').prop("disabled", false);
            break;
    }
}
4

2 回答 2

0

在事件中,您的 HTML 中有错误onClick

onClick="set_selected(\'Other\');"

您正在使用双引号"来包装,onClick但是您正在使用转义的单引号'来分隔字符串Other。没有必要在这里逃脱它们;仅当您再次使用双引号时(\"Other\")。删除它们,它应该可以工作:

onClick="set_selected('Other');"

请注意,对于 和 的其他出现,此错误会重复(\'LiveMass\')出现(\'Other\')

演示

于 2012-09-11T00:10:06.360 回答
0

我发现如果我有这个,它会起作用!

<input type="radio" id="select_LiveMass" name="source_select" value="LiveMass" CHECKED onClick="set_selected(\"LiveMass\");" style="margin-right: 5px;"/>LiveMass<br/>
<input type="radio" id="select_Other" name="source_select" value="Other" onClick="set_selected(\"Other\");" style="margin-right: 5px;"/>Other

因此set_selected(\'LiveMass\');,我没有使用转义的单引号,而是使用set_selected(\"LiveMass\");了转义的双引号,这很有效。我想了解为什么。

于 2012-09-15T17:45:54.950 回答