函数参数的值错误

Wrong value for function parameter

本文关键字:错误 参数 函数      更新时间:2023-09-26

运行脚本时,此javascript函数参数的值不正确。如果我添加一个alert语句来打印参数值,它看起来是正确的,并且脚本运行正常。什么可以解释这一点?

#ADDED

<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;
    }
}

您在HTML中的onClick事件上出现错误:

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

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

onClick="set_selected('Other');"

注意,该错误对于(''LiveMass'')(''Other'')的其它位置重复。

演示

我发现如果我有这个,它就可以工作了!

<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'");,这很有效。我想知道为什么。