js获取radio的值

js获取radio的值,第1张

<html>

<head>

</style>

<script type="text/javascript" language="javascript">

function Permissions()

{ var a = documentgetElementsByName("radios");

for(i=0;i<alength;i++)

{

if(a[i]checked)

alert(a[i]value);

}

}

</script>

</head>

<body>

<input type="radio" id="radios" name="radios" value="0" onclick="Permissions()" >Admin

<input type="radio" id="radios" name="radios" value="1" onclick="Permissions()">User

</body>

</html>

Jquery提供的选择器极大的方便了开发人员对Dom的 *** 作,真正实现了代码简化,却功能强大的目标。下面就日常最常用的,在Form表单中如何获取被中选的Radio值做一小小的示例。

form表单如下:

<form name='form1' action="#" method="post">

此处略去200字

<input type="radio" name="opType" value="0" />搁置<br />

<input type="radio" name="opType" value="1" />解决<br />

<input type="radio" name="opType" value="2" />转派4<br />

</form>

那么如何获取被选中的radio值呢,Juqery为我们提供了如下几个方法

$("input[name='opType']:checked")val() -------此方法估计用的比较多,通俗易懂

$("input:radio:checked")val(); ---------此方法最简单,但是连着使用选择器不容易懂

$("input[@name='opType'][checked]"); --------次方法中切记写成[@checked=checked],本人第一次就写成这个了

那么,偶尔也需要遍历一下radio,如何做呢?当然需要each出场了,具体如下:

$('input[name="opType"]')each(function(){

alert(thisname+thisvalue);

});

应该明白了吧,若有什么更好的方法欢迎盖楼。

1,获取RadioGroup控件:

RadioGroup radioGroup = (RadioGroup)findViewById(RidmyRadioGroup);

2,获取RadioButton控件;

RadioButton radioButton = (RadioButton)findViewById(radioGroupgetCheckedRadioButtonId());

3,获取选中的radio的值:

String text = radioButtongetText()toString();

4,为radioGroup添加监听事件,用来监听组件内部的事件响应:

radioGroupsetOnCheckedChangeListener(new RadioGroupOnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

//在这个函数里面用来改变选择的radioButton的数值,以及与其值相关的 //任何 *** 作,详见下文

selectRadioBtn();

}

})

;

5,在onCreat中需要初始化上面的四条信息;

6,整体的使用样例:

布局文件xml中的内容:

<RadioGroup

android:id="@+id/sex_group"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<RadioButton

android:id="@+id/male"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checked="true"

android:text="男"/>

<RadioButton

android:id="@+id/female"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="女"/>

</RadioGroup>

代码实现:

private RadioGroup mSex_group;

private RadioButton mMale;

private RadioButton mFemale;

private String sexName;

mSex_group = (RadioGroup) findViewById(Ridsex_group);

mMale = (RadioButton) findViewById(Ridmale);

mFemale = (RadioButton) findViewById(Ridfemale);

mSex_groupsetOnCheckedChangeListener(new RadioGroupOnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

if (mMalegetId() == checkedId) {

sexName = mMalegetText()toString();

} else if (mFemalegetId() == checkedId) {

sexName = mFemalegetText()toString();

}

}

});

<input type="radio" name="address" value="0" />

<input type="radio" name="address" value="1" />

<input type="radio" name="address" value="2" />

js中 : var addressID = $("input[name='address']:checked")val();

以上就是关于js获取radio的值全部的内容,包括:js获取radio的值、如何使用Jquery获取Form表单中被选中的radio值、如何获取RadioGroup中RadioButton的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9541834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存