使用 jquery 获取选择值和单选值

发布于 2024-09-14 12:23:06 字数 1104 浏览 4 评论 0原文

我有以下一组用于无线电和选择框的 html,我想获取用逗号分隔的值

FOR RADIO BUTTON

<div class="element" id="radio_gender">
    <div class="fleft">
        <label for="gender">Gender</label>
    </div>
    <div class="fleft">
        <input type="radio" value="Male" name="gender"  />Male
        <br />
       <input type="radio" value=" Female" name="gender"  /> Female<br />
   </div>
   <div class="clr"></div>
</div>

FOR SELECTBOX

<div class="element" id="select_gender2">
     <div class="fleft">
        <label for="gender2">Gender2</label>
     </div>
     <div class="fleft">
         <select name="gender2" id="gender2" >
             <option value="Male">Male</option>
             <option value=" Female"> Female</option>
         </select>
     </div>
     <div class="clr"></div>
</div>  

对于上述情况我需要一个字符串比如男、女

I have the following set of html for both radio and select box i want to get the value with comma seperated

FOR RADIO BUTTON

<div class="element" id="radio_gender">
    <div class="fleft">
        <label for="gender">Gender</label>
    </div>
    <div class="fleft">
        <input type="radio" value="Male" name="gender"  />Male
        <br />
       <input type="radio" value=" Female" name="gender"  /> Female<br />
   </div>
   <div class="clr"></div>
</div>

FOR SELECTBOX

<div class="element" id="select_gender2">
     <div class="fleft">
        <label for="gender2">Gender2</label>
     </div>
     <div class="fleft">
         <select name="gender2" id="gender2" >
             <option value="Male">Male</option>
             <option value=" Female"> Female</option>
         </select>
     </div>
     <div class="clr"></div>
</div>  

for bothe the above case i need a string like Male, Female

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫瑟鸿黎 2024-09-21 12:23:06

对于单选按钮:

var values = $("input:radio[name='gender']").val(); // selects all radios with name
                                                    // gender and gets their values
var string = values.join(",");

对于选择框:

var values = $("#gender2 option").val(); // finds all option tags under id 
                                                 // gender2
var string = values.join(",");

编辑:给出以下评论(对照 jquery 文档进行检查,对最初的错误感到抱歉),尝试 -

var values = [];
$("input:radio[name='gender']").each(function() {
    values.push($(this).val());
});
var string = values.join(",");

var values = [];
$("#gender2 option").each(function() {
    values.push($(this).val());
});
var string = values.join(",");

For the radio button:

var values = $("input:radio[name='gender']").val(); // selects all radios with name
                                                    // gender and gets their values
var string = values.join(",");

For the select box:

var values = $("#gender2 option").val(); // finds all option tags under id 
                                                 // gender2
var string = values.join(",");

EDIT: Given the below comments (which check out against the jquery docs, sorry about the initial mistake), try -

var values = [];
$("input:radio[name='gender']").each(function() {
    values.push($(this).val());
});
var string = values.join(",");

and

var values = [];
$("#gender2 option").each(function() {
    values.push($(this).val());
});
var string = values.join(",");
つ低調成傷 2024-09-21 12:23:06
<input type="radio" name="gender" value="male"/>
<input type="radio" name="gender" value="female"/>
<script>
    var genderVal = $("input:radio[name='gender']:checked").val();
    alert(genderVal);
</script>

如何获取选中的单选按钮值?

<input type="radio" name="gender" value="male"/>
<input type="radio" name="gender" value="female"/>
<script>
    var genderVal = $("input:radio[name='gender']:checked").val();
    alert(genderVal);
</script>

How to get checked radio button value?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文