简体   繁体   English

单击RadioButton时关联的文本框

[英]Text Box associated on click of RadioButton

Plzz someone tell me what's the problem in my code below ...as the first function(changeME ) is working BUT THE 2ND function (inputName ) is not working....is there some problem with the variables?? 请有人告诉我下面的代码是什么问题...因为第一个函数(changeME )正在工作,但是第二个函数(inputName )无法工作....变量是否存在某些问题? As m New in asp.net plzz help me out with some basic concept which I might be missing. asp.net中的m新增功能请帮助我提供一些我可能会缺少的基本概念。

 `PROJECT
  <input type="radio" name="portal" id="radio1" onclick="changeMe(this);"/> &nbsp; <input type="text" name="textprojectOff" id="text1" value="Project Name" onclick="changeMe(this);"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 


<script type="text/javascript" >
    function changeMe(inField) {
        var fieldId = inField.id;
        var type = fieldId.substring(0, 4);

        if (type == 'text') {
            var name = fieldId.substring(4);
            var radioButton = document.getElementById("radio" + name);
            radioButton.checked = true;
        } else {
            var name = fieldId.substring(5);
            var textField = document.getElementById("text" + name);
            textField.focus();
        }
    }
</script>
NON-PROJECT<input type="radio" id="radio2" name="portal" onclick="inputName(this)"/> &nbsp; <input type="text" name="textnonprojectOff" id="text2" value="Departement" onclick="inputName(this);"/>
<script type="text/javascript">

  function inputName(inDepartment){
       var departmentId=inDepartment.id;
       var type= departmentId.substring(0,4);

  if (type== 'text'){
      var name= inDepartment.substring(4);
      var radioButton=document.getElementById("radio"+ name);
      radioButton.checked=true;
  }
   else{
       var name=inDepartment.substring(5);
       var textfield=document.getElementById("text"+name);
       textfield.focus();
      }

</script>   

protected void RadioButton2_Checked(object sender, EventArgs e) { if (RadioButton2.Checked) { TextBox11.Visible = true; } else { TextBox11.Visible = false; TextBox11.Text = string.Empty; } } protected void RadioButton3_Checked(object sender, EventArgs e) { if (RadioButton3.Checked) { TextBox12.Visible = true; } else { TextBox12.Visible = false; TextBox12.Text = string.Empty; } }
Anyone can plzzz modify this code according to my previous requirement as my AIM is Same as previous but now I m using codebehind for it...but its not working properly.. 任何人都可以根据我之前的要求修改此代码,因为我的AIM与以前的相同,但是现在我正在使用代码隐藏功能...但是它无法正常工作。
plz chk out the fiddle given in the previous answers and kindly modify this code accordingly. 请找出先前答案中的小提琴,并相应地修改此代码。

Check this Fiddle 检查这个小提琴

function inputName(inDepartment){
       var departmentId=inDepartment.id;
       var type= departmentId.substring(0,4);

  if (type== 'text'){
      var name= departmentId.substring(4);
      var radioButton=document.getElementById("radio"+ name);
      radioButton.checked=true;
  }
  else{
       var name=departmentId.substring(5);
       var textfield=document.getElementById("text"+name);
       textfield.focus();
      }
  }

var name= departmentId.substring(4); and not var name= inDepartment.substring(4); 而不是var name= inDepartment.substring(4);

  • inDepartment is an object you are passing as a parameter . inDepartment是您作为参数传递的对象。 substring() method wont work on it. substring()方法无法使用。
  • Plus, you could've reused the same function for both the elements; 另外,您可以为两个元素重用相同的功能; no need to write separate functions. 无需编写单独的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM