加载执行时的 JavaScript
我的 PHP 页面上有一个输入字段。当在输入框中输入值时,将调用 javascript,并在输入框旁边返回 mysql 值。这 100% 有效,没有错误。
我的代码是:
<input type=text id=sku1 name=sku1 onchange="showUser(1, this.value)" onkeypress="return enter(document.orderform.sku2)" value=<? echo $sku1; ?> >
当用户提交表单时,页面会在执行一些计算时返回到自身。 当页面重新加载时,输入框包含用户输入的值($sku1)。我的问题是 mysql 值不再位于输入框旁边,因为输入框自重新加载后没有更改。
我想使用类似 javaascript OnLoad 功能的东西。
<input type=text id=sku1 name=sku1 onchange="showUser(1, this.value)" OnLoad="showUser(1, this.value)" onkeypress="return enter(document.orderform.sku2)" value=<? echo $sku1; ?> >
然而这不起作用。
关于如何让 JavaScript 在加载时以及用户更改输入框的值时执行的任何想法?
谢谢和问候, 瑞安·史密斯
I have an input field on my PHP page. When a value is entered into the input box a javascript is called and mysql values returned next to the input box. This works 100% without errors.
My code for this is:
<input type=text id=sku1 name=sku1 onchange="showUser(1, this.value)" onkeypress="return enter(document.orderform.sku2)" value=<? echo $sku1; ?> >
When the user submits the form, the page returns to itself as some calculations are performed.
When the page reloads the input box has the value the user entered ($sku1). my problem is that the mysql values are no longer next to the input box as the input box hasnt changed since reloading.
I want to use something like the javaascript OnLoad functionality.
<input type=text id=sku1 name=sku1 onchange="showUser(1, this.value)" OnLoad="showUser(1, this.value)" onkeypress="return enter(document.orderform.sku2)" value=<? echo $sku1; ?> >
This however does not work.
Any ideas as how to get the javascript to execute on load as well as when the user changes the value of the input box?
Thanks and Regards,
Ryan Smith
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用jquery的文档就绪方法或尝试将showUser放在body onload中
Use jquery's document ready method or try putting showUser in body onload
您可以像这样使用
onload
事件:You can use
onload
event like this:我建议使用 DOMContentReady,而不是 Load,因为它会更早触发。您还需要将侦听器附加到文档,而不是输入元素本身,因为输入元素不提供加载事件(http://www.w3.org/TR/html4/interact/forms.html #h-17.4)
因此,在脚本标签或单独的 javascript 文件中:
I'd suggest using DOMContentReady, rather than Load, as it will fire earlier. You'll also need to attach the listener to document, rather than the input element itself, as the input element doesn't provide a load event (http://www.w3.org/TR/html4/interact/forms.html#h-17.4)
So, in a script tag, or a separate javascript file: