简体   繁体   English

使用插入的JavaScript访问文本框值

[英]Accessing Textbox Value Using Inserted JavaScript

I'm Having a bit of an issue. 我有一个问题。 I'm using JavaScript to inset HTML into a webpage along with an event handler for the onblur event. 我正在使用JavaScript将HTML以及onblur事件的事件处理程序插入到网页中。 It looks like this: 看起来像这样:

return '<input id = "txtIn" type="text" value=" ' + dateTime + '" onblur= "submitManualDate(document.getElementById("txIn").value(), 2, 3);" />';

I'm getting a syntax error. 我收到语法错误。 However, the following works perfectly fine: 但是,以下工作完全正常:

return '<input id = "txtIn" type="text" value=" ' + dateTime + '" onblur= "submitManualDate(1, 2, 3);" />';

Any idea what I'm missing? 知道我缺少什么吗?

Using " inside " (double quoted) expression will break the string . 使用"内部" (双引号)表达式将破坏string

It's better to pass this as an argument as you should not have multiple elements with same id attributes in a document. 最好this作为参数传递,因为在文档中不应有多个具有相同id属性的元素。

Try this example: 试试这个例子:

 (function() { var input = '<input type="text" value="5" onblur= "submitManualDate(this, 2, 3);" />'; document.getElementById('parent').innerHTML = input; })(); function submitManualDate(elem, j, k) { alert(elem.value + ' ' + j + ' ' + k); } 
 <div id='parent'></div> 

Fiddle here 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM