2014-04-17 69 views
1

我試圖獲得使用ajax和jquery選擇哪個單選按鈕的值,但我只能做到以下,當然它只能工作如果該單選按鈕是否被選中。我究竟做錯了什麼?試圖獲得選定的單選按鈕使用ajax/jQuery的價值

$('#urgencyJList').change(function() { 
      var modelData = { 
       paperType: $("#paperTypeJList").val(), 
       urgency: $("#urgencyJList").val(), 
       numOfPages: $("#numOfPagesJList").val(), 
       **spacing: $("#doubleSpacingV").val()** 
      }; 

      $.ajax({ 
       url: '@Url.Action("getNewPrice","Home")', 
       data: modelData, 
       type: "POST", 
       cache: false, 
       async: true, 
       success: function (response) { 
        document.getElementById('priceLabel').innerHTML = response; 
       } 
      }); 
     }); 

我的單選按鈕,代碼如下:

<div class="row"> 
    @Html.LabelFor(model => model.spacing, "Spacing:") 
    <fieldset> 
     <label for="doubleSpacingV"> 
      Double Spacing 
      @Html.RadioButtonFor(model => model.spacing, "double", new { id = "doubleSpacingV", @checked = true }) 
     </label> 
     <label for="singleSpacingV"> 
      Single Spacing 
      @Html.RadioButtonFor(model => model.spacing, "single", new { id = "singleSpacingV" }) 
     </label> 
     @Html.ValidationMessageFor(model => model.spacing) 
    </fieldset> 
</div> 
+0

我剛剛用創建單選按鈕的代碼編輯它 –

+0

我該如何使用我的代碼?沒有辦法獲得任何單選按鈕的檢查價值嗎? –

回答

3

改變你的HTML作爲,在這裏,我增加的公共類spacingCheckBox以單選按鈕

<fieldset> 
    <label for="doubleSpacingV"> 
     Double Spacing 
     @Html.RadioButtonFor(model => model.spacing, "double", new { @class = "spacingCheckBox", @checked = true }) 
    </label> 
    <label for="singleSpacingV"> 
     Single Spacing 
     @Html.RadioButtonFor(model => model.spacing, "single", new { @class = "spacingCheckBox" }) 
    </label> 
    @Html.ValidationMessageFor(model => model.spacing) 
</fieldset> 

要檢索選中的複選框值使用

spacing: $('.spacingCheckBox:checked').val() 

注意:我還沒有測試過

+0

它的工作原理!謝謝 –

+0

謝謝,它的工作原理 –