功能参数的值错误

时间:2012-09-10 22:38:10

标签: javascript

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

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

2 个答案:

答案 0 :(得分:0)

您在onClick事件中的HTML中出错:

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

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

onClick="set_selected('Other');"

请注意,(\'LiveMass\')(\'Other\')的其他事件会重复此错误。

<强> DEMO

答案 1 :(得分: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\");,这样可行。我想了解原因。