English 中文(简体)
j Query Ajax How Do I Pass responseText aserra to indexOf?
原标题:jQuery Ajax How Do I Pass responseText as variable to indexOf?

我正试图从Ajax请求的复函中附上一页内容。

在预订之前,我希望看到,复函案文是否已经在括号中存在,如果是的话,不增加。 问题在于,我无法利用变数的对应办法开展工作。 我的法典在我通过一个直面字典,以指数化时发挥作用。

我的守则是:

jQuery( #addButton ).live( click , function () {

    new Ajax( <url> , {
        method:  get ,
        dataType:  html ,
        onSuccess: function (responseText) {
            var text = jQuery( #div ).text();

            if (text.indexOf(responseText) == -1) {
                //always true using responseText; 
                //string literal works though
                jQuery( #div ).append(responseText);
            }
        }
    }).request();

})

事先感谢任何建议。

最佳回答

与j Query核对答复,并尝试与text()代表对比......

var resp = $(responseText);
var div = $("#div");

if(div.text().indexOf(resp.text()) == -1)
  resp.appendTo(div)
问题回答

或许是因为你在平原案文中重新寻找html。 使用<代码>html代替text:

var text = jQuery( #div ).html();
if (text.indexOf(responseText) == -1) {
   ...

以上所述的解决办法对我来说(jQuery 1.11.2)。

但我最后找到了我的解决方法:$(my_variable).selector

它对我来说毫无问题:

var responseText = "word";
var resp = $(responseText).selector;



$("div").each(function() {
  if ($(this).text().indexOf(resp) >= 0) {
    alert("I ve found your query: " + resp + " at " + $(this).offset().left + "/" + $(this).offset().top);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>THis is a test</div>
<div>I m searching for this word</div>
<div>nothing here</div>




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签