簡體   English   中英

從加載在索引文件中的ajax文件獲取keyup事件的值

[英]get value on keyup event from ajax file loaded in index file

php和getvalue.php聽到getvalue.php是通過ajax調用的。

Index.php的內容是...

function getthis(str) {
                      var xhttp; 

                      if (str == "") {
                        document.getElementById("getthis").innerHTML = "";
                        return;
                      }
                      xhttp = new XMLHttpRequest();
                      xhttp.onreadystatechange = function() {
                        if (this.readyState == 4 && this.status == 200) {
                          document.getElementById("getthis").innerHTML = this.responseText;
                        }
                      };
                      xhttp.open("GET", "get.php?str="+str, true);
                      xhttp.send();
                    }   

上面的腳本是ajax調用。

和index.php內容如下

<div id="abc">
<select name="abx" onChange="getthis(this.value)">
<option value="1" data-id="1">1</oprion>
<option value="2" data-id="2">2</option>
</select>
</div>
<div id="getthis">
</div>

現在在獲取value.php是以下代碼

 <?php

 $_GET['str'];

 ?>

 <input type="text" value="<?php echo $_GET['str']; ?>" class="thisis"> 

現在通過跟隨scripct在index.php上,我想通過此代碼從keyup事件上的輸入框中獲取值,但是我還沒有得到任何值,為什么...?

<script type="javascript">
$(document).ready(function(){

    $('#getthis input.thisis').keyup(function(){
         var abc = $(this).val();
        alert(abc);

    });

    });
</script>

加載時,dom元素不在其中。 一旦處理了Ajax請求,就會填充它們。

在ajax請求完成后,不妨嘗試綁定元素onload,而不僅僅是嘗試綁定該元素。

 if (this.readyState == 4 && this.status == 200) { document.getElementById("getthis").innerHTML = this.responseText; functionbind(); } var functionbind = function(){ $('#getthis input.thisis').keyup(function(){ var abc = $(this).val(); alert(abc); }); $('select ').keyup(function(){ abc = $(this).val(); alert(abc); }); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM