简体   繁体   English

如何在文本字段输入上执行javascript函数并用新值更新字段

[英]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.

相关问题 如何在按回车键时执行 function<input> 场地? - How can I execute a function on pressing the enter key in an <input> field? 如何在javascript函数中访问输入文本字段的值? - How to access the value of the input text field in javascript function? 如何使用 JavaScript 获取文本输入字段的值? - How do I get the value of text input field using JavaScript? 使用javascript更新输入字段值 - Update Input field value with javascript 如何检查按键上文本字段的新值? - How can I check the new value of a text field on keypress? 如何在 JavaScript 中获取检查的行输入字段值? - How can i get checked row input field value in JavaScript? 为什么我不能在其中输入文字<input>带有 javascript 代码的字段? 文字闪回原值 - Why can't I input text into this <input> field with javascript code? The text flashes back to original value 如何为JavaScript函数创建文本字段? - How can I make a text field for a JavaScript function? 如何使用更新按钮使用JavaScript函数更改输入字段的文本? - How to use update button to change the text of an input field using a JavaScript function? 使用JAVASCRIPT转换货币。 无法通过我的函数设置转换后的输入文本字段的值 - Converting currencies using JAVASCRIPT. can't set the value of converted input text field through my function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM