简体   繁体   English

在iframe中分配变量

[英]Assign a variable inside a iframe

After looking for a few days this answer comes close but still wont fix my problem jQuery: How to assign a variable inside of .html 在寻找了几天之后,这个答案接近了,但仍然无法解决我的问题jQuery:如何在.html中分配变量

I'm trying to get the vaule of the textbox into the scr part of a frame. 我正在尝试将文本框的有效内容放入框架的scr部分。 I understand that from the above link that javascript must pull out the variable or it treats it as text but after trying for a few days cant get it to work. 我了解到,从以上链接可以看出,javascript必须将变量拉出或将其视为文本,但是尝试了几天后仍无法正常工作。 The response lands in a div on the page 响应位于页面上的div中

<script>
$(document).ready(function() {
  $("#q").bind(
      "webkitspeechchange", 
      function(evt) {
        $('.response').html(
          "<iframe width='625' height='250' border='0' src='/index.php?q=' + INPUT_HERE + ' /><br> voice search vaule:"+ $(this).val())
        .fadeIn();   
}); 
});
</script>

This works for me: 这对我有用:

<script>
$(document).ready(function(){
    $('#q').on( 'webkitspeechchange', function(e) {
        $('#response').html('<iframe width="625" height="250" border="1" src="/index.php?q=' + $(this).val() + '" /><br> Voice search value: ' + $(this).val()).fadeIn();
    });
});
</script>

<input type="text" name="q" id="q" x-webkit-speech="x-webkit-speech">
<div id="response"></div>

And here is the JS Fiddle 这是JS小提琴

If you want to remove the surrounding div you can call $('#response').replaceWith(... instead of $('#response').html(... , but be aware that successive searches won't work anymore because #response has been removed from the DOM. 如果您要删除周围的div ,则可以调用$('#response').replaceWith(...而不是$('#response').html(... ,但是请注意,连续搜索将无法进行不再是因为#response已从DOM中删除。

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

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