当文本框以编程方式更改时,javascript 触发事件
我有一个应用程序,其中服务器正在向我需要编程的客户端发送数据。每个数据字符串都被发送到一个唯一的文本框。我想捕获这些数据并在显示之前根据独立可选列表的状态对其进行格式化。我无法使用 onchange 触发事件,因为它仅适用于用户输入。有没有一种简单的方法可以做到这一点?我找不到一个直接的解决方案。
<input class="dios" id="diosinput" type="text" value="" data-ref="#source" data-path="/abc/0/input" />
<select class="widthed">
<option>Hex</option>
<option>Binary</option>
</select>
//my function
function decToHex(d)
{
var displayHex = d.toString(16);
if(displayHex.length<2) displayHex = '0'+ displayHex;
return '0x'+ displayHex.toUpperCase();
}
I have an application in which server is sending data to client that I need to program. Each string of data is sent to a unique text box. I want to catch this data and format it before displaying depending on state of independent selectable list. I cannot fire an event using onchange as it works only for user's input. Is there an easy way of doing that? I couldn't find one straight solution for this.
<input class="dios" id="diosinput" type="text" value="" data-ref="#source" data-path="/abc/0/input" />
<select class="widthed">
<option>Hex</option>
<option>Binary</option>
</select>
//my function
function decToHex(d)
{
var displayHex = d.toString(16);
if(displayHex.length<2) displayHex = '0'+ displayHex;
return '0x'+ displayHex.toUpperCase();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
从服务器更改文本框只有两种方法:
当浏览器加载整个页面时
当浏览器向服务器发送 AJAX 请求并替换时输入元素中的文本
Web 服务器无法“将数据发送到文本框”。你可能想说的是别的东西,但你缺少正确的词语来表达它。
我从源代码中可以看到的是
data-ref
和data-path
。这表明 AJAX 用于从服务器获取新值并将其放入 DOM 中。要更改此设置,请查看页面的整个源代码(包括所有 JavaScript 代码)并查找
path
或data-path
或data('path' )
或data("path")
如果你找到发出请求的地方,你就会知道在哪里添加你自己的回调。
There are only two way to change a text box from the server:
When the browser loads the whole page
When the browser sends an AJAX request to the server and replaces the text in the input element
There is no way for a web server to "send data to a text box". You probably mean something else but you're missing the right words to express it.
What I can see from the source is
data-ref
anddata-path
. This suggests that AJAX is used to fetch a new value from the server and put it into the DOM.To change this, look at the whole source code of the page (including all JavaScript code) and look for
path
ordata-path
ordata('path')
ordata("path")
If you find the place where the request is made, you will know where to add your own callback.