Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 文本框仅在选中第一个单选按钮时接受数字_Javascript_Jquery - Fatal编程技术网

Javascript 文本框仅在选中第一个单选按钮时接受数字

Javascript 文本框仅在选中第一个单选按钮时接受数字,javascript,jquery,Javascript,Jquery,我有四个单选按钮,我想当用户点击第一个单选按钮,输入文本框只能接受数字,但如果他们选择其他单选按钮,数字和文本只能接受。下面是我的代码: <input class="form-control" type="text" id="usr_matriculation" maxlength="40" name="usr_matriculation" required="required"> <div class="radio"> <label cl

我有四个单选按钮,我想当用户点击第一个单选按钮,输入文本框只能接受数字,但如果他们选择其他单选按钮,数字和文本只能接受。下面是我的代码:

<input class="form-control" type="text" id="usr_matriculation" maxlength="40" name="usr_matriculation" required="required">

    <div class="radio">
        <label class="radio-inline"><input type="radio" onclick="il.Form.showSubForm('subform_usr_id_type_1', 'usr_id_type', null);" name="usr_id_type" id="usr_id_type_1" value="1">
        New Identity Card No.</label>

    </div>


    <div class="radio">
        <label class="radio-inline"><input type="radio" onclick="il.Form.showSubForm('subform_usr_id_type_2', 'usr_id_type', null);" name="usr_id_type" id="usr_id_type_2" value="2">
        Passport No.</label>

    </div>


    <div class="radio">
        <label class="radio-inline"><input type="radio" onclick="il.Form.showSubForm('subform_usr_id_type_3', 'usr_id_type', null);" name="usr_id_type" id="usr_id_type_3" value="3">
        Army No.</label>

    </div>


    <div class="radio">
        <label class="radio-inline"><input type="radio" onclick="il.Form.showSubForm('subform_usr_id_type_4', 'usr_id_type', null);" name="usr_id_type" id="usr_id_type_4" value="4">
        Police No.</label>

    </div>

我的小提琴:

当您单击第一个按钮时,它会添加一个事件,查找
$('input[name=“usr_preciculation”]”)的
键(
,因此每次您不能键入文本时,只需输入数字。

这可能不是最好的解决方案,但我做了一些测试

<input type="text" id="i">

<input checked type="radio" id="r_n" name="rn">
<input type="radio" id="r" name="rn">
<input type="radio" id="r" name="rn">
<input type="radio" id="r" name="rn">

单击其他侦听器时,只需删除该侦听器。它与单选按钮一起工作吗?谢谢,我只想问一件事,如果他们在单击单选按钮之前在文本框中输入文本,如何删除所有文本?您可以使用“onfocus”或“onclick”事件来处理它<代码>$(“#i”)。单击(函数(){$(this.val(“”);})我也这么认为,但在更改收音机选项时,似乎还有另一个问题。
<input type="text" id="i">

<input checked type="radio" id="r_n" name="rn">
<input type="radio" id="r" name="rn">
<input type="radio" id="r" name="rn">
<input type="radio" id="r" name="rn">
$("#i").keyup(function () {
  if ($('#r_n').is(':checked')) {
  console.log("Validating #r1...")
  // Filter non-digits from input value.
  this.value = this.value.replace(/\D/g, '');
}
else {
  console.log("Validating the other radios")
  this.value = this.value.replace(/[^A-Za-z0-9]+/g, '');
}
});
// Optional: Delete input value when radio changes
$('input:radio[name="rn"]').change(
function(){
  $("#i").val('');
});