2010-04-23 147 views
2

我有以下字段條件檢查:單選按鈕在jquery的

  <label>Company Type:</label> 
      <label for="primary"><input onclick="javascript: $('#sec').hide('slow');$('#primary_company').find('option:first').attr('selected','selected');" type="radio" runat="server" name="companyType" id="primary" />Primary</label> 
      <label for="secondary"><input onclick="javascript: $('#sec').show('slow');" type="radio" runat="server" name="companyType" id="secondary" />Secondary</label> 
      <div id="sec"> 
      <fieldset> 
      <label for="primary_company">Primary Company:</label> 
      <%= Html.DropDownList("primary_company", Model.SelectPrimaryCompanies, "** Select Primary Company **") %> 
      </fieldset> 

如果存在primary_company,則次級單選按鈕應該被選中。 如果沒有primary_company,那麼應該選擇主要單選按鈕。

這裏是我的jQuery:

$(document).ready(function() { 
     if ($('#primary_company').val().length > 0) { 
      $('#secondary').attr({ checked: true }); 
     } 
     else { 
      $('#primary').attr('checked', true); 
      $('#sec').hide(); 
     } 

的秒的div隱藏和顯示正常,但一個單選按鈕選擇永遠。 我試過.attr('checked', 'checked').attr({ checked: true }).attr('checked', true),但沒有選擇任何東西。

編輯 現在我已經試過.attr('checked', 'true') 莫不是我的IDS或許真的錯了嗎?我卡住了。

+0

你很近,只是幾個排列組合。 :) – 2010-04-23 19:56:51

回答

1

嘗試在單選按鈕的輸入標籤上刪除runat="server"

+0

謝謝!!我知道它必須是有些愚蠢! – RememberME 2010-04-23 20:57:55

3

的語法是:

$( '#次級')ATTR( '檢查', '真實');。

+0

什麼都沒有=( – RememberME 2010-04-23 19:59:52

+0

這是對的,你在做別的錯誤:/ – 2010-04-23 20:11:16

+0

更正式地說,'.attr('checked','checked');' – 2011-04-27 12:48:49