2012-09-10 36 views
0

腳本運行時,此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

你能提供相關的HTML嗎? –

+2

而不是使用'alert'來測試這個值,試試'console.log'並檢查你的控制檯,看看有什麼進來。 – temporalslide

+0

在switch語句中,也添加默認情況,看它是否跳過這兩種情況,而是進入默認情況下。 –

回答

0

您的HTML錯誤,在onClick事件:

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

你是使用雙引用"來包裝onClick但後來您正在使用轉義單引號'劃定字符串Other。沒有必要在這裏逃避它們;只有當你再次使用雙引號(\"Other\")。刪除它們,它應該工作:

onClick="set_selected('Other');" 

注意,對於(\'LiveMass\')(\'Other\')其他ocurrences重複這個錯誤。

DEMO

+0

這不可能是問題所在。我應該可以逃避單引號。無論如何,我必須逃避它們,因爲表單信息是在單引號的變量中定義的。 – user823527

+0

@ user823527:你看過我的演示了嗎?我得到的語法錯誤沒有不引用它們。 –

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\");與轉義雙引號來完成這項工程。我想明白爲什麼。