views:

51

answers:

3

hi..everyone..i want after i type "0203-ED" in textfield ...two character behind that text can control the radio button.. "ED" character from that text can make one radiobutton which has value="ED" are checked...

this is my code:

<script type="text/javascript">
      var model=$("#tags1").val();
      var version[0,0]="JD";
      var version[0,1]="87.5-107.9";
      var version[1,0]="ED";
      var version[1,1]="87.5-108.0";
      var version[2,0]="EED";
      var version[2,1]="65.0-74.0";

     // each version
      for (var i = 0; i < version.length; i ++) {
          if (model.lastIndexOf(version[i,0])!=-1) {
              $("#value").replaceWith("<div id='value'>"+version[i,1]+"</div>");
          } else {
                $("#value").replaceWith("<div id='value'></div>")
          }
      // end each
      }
  </script>

what's wrong with my code??

+1  A: 

I see a couple things going on.

First, why do you have a default: in there? That's a keyword used in switch statements; it's not valid in an if statement. Remove it entirely.

You also have model,lastIndexOf instead of model.lastIndexOf

VoteyDisciple
Good catch on the comma +1
alex
i already change comma to dot...but still not work..what must i do with my code??
klox
var buttons = [];//store radio buttons in the array buttons//assign onKeyUp() to `onkeyup` attribute of the text inputfunction onKeyUp(){ var input = document.getElementById("txtInput_id"); var text = input.value; if(text.length < 2) return; text = text.substring(text.length - 3); for(var i = 0; i < buttons.length; i++) { if(buttons[i].value == text) buttons[i].checked = true; }}i still confuse at "var button= [];"
klox
+1  A: 

you don't have to use .replaceWith()...

 for (var i = 0; i < version.length; i ++) {
      if (model.lastIndexOf(version[i,0])!=-1) {
          $("#value").html(version[i,1]);
      } else {
       //default
          $("#value").html("");
      }
  // end each
  }
Reigel
what about this ?var buttons = [];//store radio buttons in the array buttons//assign onKeyUp() to `onkeyup` attribute of the text inputfunction onKeyUp(){ var input = document.getElementById("txtInput_id"); var text = input.value; if(text.length < 2) return; text = text.substring(text.length - 3); for(var i = 0; i < buttons.length; i++) { if(buttons[i].value == text) buttons[i].checked = true; }}
klox
A: 

Watch your language typing. It should be text/javascript. Right now you have text/jaavascript. Depending on the browser and your doctype, this could prevent your script from even executing (it wouldn't just fail silently, it wouldn't even get the chance to fail).

In addition to the answers already given, try declaring your array only once. There are several ways create the data structure you're after. Use whichever style you find most comfortable.

var version = [['JD',"87.5-107.9"],["ED","87.5-108.0"],["EED","65.0-74.0"]];

var version = [];
version[0] = ["JD","87.5-107.9"]
version[1] = ["ED","87.5-108.0"];
version[2] = ["EED","65.0-74.0"];

var version = [];
version[0] = [];
version[0,0]="JD";
version[0,1]="87.5-107.9";
version[1] = [];
version[1,0]="ED";
version[1,1]="87.5-108.0";
version[2] = [];
version[2,0]="EED";
version[2,1]="65.0-74.0";

About the third version, though, I'm actually not sure that your array notation (version[0,1]) is universally supported. I don't think I've seen anyone use that notation in more than 10 years. Typically, you access members of a multi-dimensional array with the notation: myArray[x][y]. But maybe that's just a style difference. I'm not sure.

[EDIT] Are you trying to do something like this? This isn't terribly efficient, but it may be a starting point for you. In this case, jQuery is definitely your friend.

<input type="radio" name="version" value="87.5-107.9" /><label class="version">JD</label><br />
<input type="radio" name="version" value="87.5-108.0" /><label class="version">ED</label><br />
<input type="radio" name="version" value="65.0-74.0" /><label class="version">EED</label><br />
<input type="text" name="version_text" value="Enter your value" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">//<![[
var jVersionTextInput = $('input[name=version_text]');
var jLabels = $('label.version');
var jRadioButtons = $('input[name=version]');
var intVersionCount = jLabels.size();
var updateRadioButtons = function() {
    var rxLabelValue;
    var isValueFound = false;
    rxLabelValue = new RegExp( $(jVersionTextInput).val() );
    for (var i = 0; i < intVersionCount; i++) {
        if (!isValueFound && rxLabelValue.test($(jLabels[i]).html())) {
            jRadioButtons[i].checked = true;
            isValueFound = true;
        }
        else {
            jRadioButtons[i].checked = false;
        }
    } 
}
jVersionTextInput.keyup(updateRadioButtons);
//]]></script>
Andrew
what about this ? var buttons = []; //store radio buttons in the array buttons //assign onKeyUp() to onkeyup attribute of the text input function onKeyUp() { var input = document.getElementById("txtInput_id"); var text = input.value; if(text.length < 2) return; text = text.substring(text.length - 3); for(var i = 0; i < buttons.length; i++) { if(buttons[i].value == text) buttons[i].checked = true; } }could i control radiobutton from textfield value?
klox
That could work. I think you would want to go through all the buttons and uncheck all buttons except the first valid match. You could also use a regex to more flexibly check for a valid match.
Andrew
can u recommend your code using regex?? coz i've never been using it..
klox
I've updated my answer with more code. If you want it to be case insensitive, add a second parameter to the definition like this: new RegExp('the regular expression','i');
Andrew