2017-09-25 52 views
0

我試圖選擇從包括檢查單選按鈕(一個或多個)的值的形式的所有輸入值,使用下面的代碼的按鈕:選擇與其他輸入沿檢查單選按鈕值

$("form#second :input:not(:button)").each(function() { 
     values += $(this).val() + "\n"; 
}); 
console.log(values); 

我知道我可以使用類似這樣的方式檢查單選按鈕的值:

$("form#second :input[name=radioBtn]:checked) 

有沒有一種方法可以將兩者結合起來?獲取檢查單選按鈕的所有輸入包括價值

<form id="second"> 
<select> 
    <option value="val">Value</option> 
</select> 
    <input type="button" value="Del" > 
    <button id="btnAdd" class="button" type="submit" >Add</button> 
    <textarea id="csTopic" name="comment">Some value</textarea> 
    <input type="radio" id="yes" name="radioBtn" value="yes"> 
    <input type="radio" id="no" name="radioBtn" checked value="no"> 
</form> 
+0

您可以提供您的表單代碼! –

+0

@DavidJorHpan HTML添加 – hello

回答

0

jQuery有一個非常有用的功能,在這種情況下使用 - serialize()

演示:http://jsfiddle.net/55xnJ/2/

​​

正如你看到的上面,所有的具有name屬性的表單中的元素將自動添加到帶有&分隔符的字符串中。

注意:由於沒有使用按鈕提交表單,所以沒有提交按鈕值被序列化。對於要包含在序列化字符串中的表單元素的值,元素必須具有name屬性。僅當檢查複選框和單選按鈕(「收音機」或「複選框」類型的輸入)時,纔會包含這些值。來自文件選擇元素的數據不會被序列化。

0

正如你所提到的,得到所有輸入,包括選中的單選按鈕的值。通過formId input形成所有輸入的循環,並使用each作爲循環的所有輸入!然後你可以檢查根據您的要求,下面是我的榜樣......

var val = []; 
 
$("#second input").each(function() { 
 
    switch($(this).attr("type")) { 
 
     //radio type 
 
     case "radio" : 
 
     if($(this).is(":checked")) { 
 
      val.push($(this).val()); 
 
     } 
 
     break; 
 
     //other type is set default 
 
     default : 
 
      val.push($(this).val()); 
 
     break; 
 
     } 
 
}); 
 
console.log(val);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form id="second"> 
 
<select> 
 
    <option value="val">Value</option> 
 
</select> 
 
    <input type="button" value="Del" > 
 
    <button id="btnAdd" class="button" type="submit" >Add</button> 
 
    <textarea id="csTopic" name="comment">Some value</textarea> 
 
    <input type="radio" id="yes" name="radioBtn" value="yes"> 
 
    <input type="radio" id="no" name="radioBtn" checked value="no"> 
 
</form>

相關問題