开发者

Unable to get the value of 2 textboxes

I'm trying to get the value of 2 textboxes with Javascript.

Textbox 1, nothing wrong.

Textbox 2, same code, but nothing happens.

Here is the code

var fieldname;
fieldname = document.getElementById("div"+field).getAttribute("field");

alert(fieldname);    // RETURNS "Birthdate"
var textval;

textval = document.getElementById("textfield"+field).value;
alert开发者_StackOverflow(textval);      // RETURNS NOTHING

var field is the id of the textbox and div.

Why isn't this working?


When posting html/javascript/css problems like this, isolate the code and put it in a jsfiddle ( jsfiddle.net ). Not only will this allow others to much more quickly solve your problem for you, but often isolating the code from your environment cuts out extra factors that may have been causing unexpected behavior. Often the process of moving a snippet to jsfiddle.net can show the problem before anyone else even needs to get involved!

Secondly, I recommend modifying your debugging practices to make it easier on yourself than using alerts:

  • Run a browser with a js console.
  • Log debugging to console.log()
  • To ensure graceful degredation of console.log, add this code to your global javascript:

    if (typeof(console) == 'undefined') { console = { log: function() { } }; }

  • Run js in the console to check for values where something goes wrong in the static code, e.g.

    • run document.getElementById("textfield"+field).value to check whether the value you expect is available, and then walk back down the line if you don't find what you're expecting, e.g. if .value isn't available, run document.getElementById("textfield"+field) and expand the object to view it's contents, etc.
  • You can also run any arbitrary javascript of your choice in the console, with clickable access to the available javascript objects created by the page loading. This somewhat lessens the need for manually coding in alerts as well.


<div id="div<?php echo $aantal;?>" field="<?php echo $rows['field'];?>">
                            <h3><?php echo $rows['name'];?>:</h3>
                            <p id="p<?php echo $aantal;?>">
                                <input type="text" id="textfield<?php echo $aantal;?>" value="<?php echo $rows['default'];?>" MAXLENGTH="<?php echo $rows['length'];?>"/><br/>
                                <input type="button" id="buttonacc<?php echo $aantal;?>" value="Verzend" onClick="acc('<?php echo $aantal;?>');"/>
                                <input type="button" id="buttonden<?php echo $aantal;?>" value="Nu niet!" onClick="den('<?php echo $aantal;?>');"/>
                            </p>
</div>

This is the HTML


javascript:var val = 1;alert(document.getElementById("textfield"+val).value)

When i use this code in the javaconsole of firefox, nothing wrong!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜