开发者

JavaScript not working with Chrome & Xampp!

I've been trying for a couple hours now to figure out why JavaScript wouldn't work. The code works, but here it is anyway.

<sc开发者_运维百科ript type="text/javascript">
function change(text)
{
document.f1.ta.value="Hi!";
}
</script>
<form name="f1">
<input type="textarea" id="ta"/>
<input type="button" action='change("Hi!")'/>
</form>

When I click the button, it does nothing. When I write "document.f1.ta.value="Hi!";" in the Chrome's inspector console, it works. I am using XAMPP (for Windows) 1.7.3 Windows 7 Ultimate.


Your button is using "action" - that should be "onclick" for an element itself..

and/or

document.f1.ta.value="Hi!"; is failing... try

function test() {
   alert('test');
}

and add

<button onclick="test();">Test</button>

to your body


Two things:

You have specified an action attribute on the button, I think you are looking for the onclick intrinsic event:

<input type="button" onclick='change("Hi!")'/>

The standard way (DOM0) to access forms and form elements would be:

function change(text) {
  document.forms[0].elements.ta.value = text;
}

Check an example here.


That is not a standard way of accessing elements. Use document.getElementsByName or document.getElementById.

document.getElementById("ta").value="Hi!";

As noted by CMS, you also want onclick for the button.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜