[英]How can I execute a javascript function on text field input and update the field with new value
I use a javascript function to find the src string in an iframe tag and return the found src string, and it works correctly alone, but I need to use it on a text input field to grab the iframe src on input paste, and update the same field with the new value returned by the function. 我使用JavaScript函数在iframe标记中找到src字符串并返回找到的src字符串,并且它可以单独正常运行,但是我需要在文本输入字段上使用它来在输入粘贴中获取iframe src,并更新与该函数返回的新值相同的字段。
The search function works correctly 搜索功能正常工作
function findSrc(str, pointa, pointb)
{
var str = " "+str;
var pa = str.indexOf(pointa);
if( pa == 0 ) return "";
pa += pointa.length;
var pb = str.indexOf(pointb,pa) - pa;
return str.substr(pa,pb);
}
Method used for text field but fails with no error message in the console 用于文本字段的方法,但在控制台中失败并没有错误消息
var fld = document.getElementById('textfield');
var src = fld.value;
fld.oninput = function() {
if( src.indexOf('<iframe') != -1 ) {
fld.value = findSrc(src, 'src="', '"');
}
}
HTML 的HTML
<input type="text" id="textfield" name="embedsrc" size="50" value="" />
What would be the correct method to achieve my goal? 什么是实现我的目标的正确方法?
I got it working with the following method, however if there is a better or efficient way, please inform. 我使用以下方法进行操作,但是,如果有更好或有效的方法,请告知。
The same string search function 相同的字符串搜索功能
function findSrc(str, pointa, pointb)
{
var str = " "+str;
var pa = str.indexOf(pointa);
if( pa == 0 ) return "";
pa += pointa.length;
var pb = str.indexOf(pointb,pa) - pa;
return str.substr(pa,pb);
}
Created a new function 创建了一个新功能
function getsrc()
{
var fld = document.getElementById('textfield');
var src = fld.value;
if( src.indexOf("<iframe") != -1 ) {
fld.value = findSrc(src, 'src="', '"');
}
}
HTML Appended addEventListener to the field with call back to the new function HTML将addEventListener附加到该字段,并回调新函数
<input id="textfield" name="frmsrc" type="text" size="50" value="" />
<script>
document.getElementById("textfield").addEventListener("input", getsrc);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.