简体   繁体   English

如何将jquery元素text()转换为字符串

[英]how to convert the jquery element text() into string

I can not successfully get the string using the jquery text() I have my HTML 我无法使用jquery text()成功获取字符串,但我拥有HTML

 <span>/</span>
            <span onclick="
            var inner_text=$.trim($(this).text());

        if(inner_text=='Send Request'){

        var post_id=<?php echo $blog_id  ;?>;

        var event_date='<?php echo $blog_feeds['valid_date'] ?>';
        var post_text='<?php echo addslashes($blog_feeds['post_text'])  ?>'; //if the string contains ' , then would have error
        var request_text_id='<?php echo $blog_id.'comment_text';?>';


        var request_content=$('#'+request_text_id).val();
        var who_post='<?php echo $blog_feeds['username']  ;?>';


        $.ajax({
        url:'php/join_request.php',
        method:'post',
        data:{request_content:request_content,post_id:post_id,who_post:who_post,event_date:event_date,post_text:post_text},
        success:function(data){

        if(data=='ok'){


            $('<?php echo '#'.$blog_id.'request_w' ?>').html('You have sent '+who_post+' a joining reuqest on this event');
            $('<?php echo '#'.$blog_id.'request_button'; ?>').html('Request Sent');
            $('<?php echo '#'.$blog_id.'request_button'; ?>').css('opacity','0.7');
            $('<?php echo '#'.$blog_id.'request_button'; ?>').css('cursor','initial');
            $('<?php echo '#'.$blog_id.'comment_text';?>').val('');
        }


        }
        })
        }
        "    id="<?php echo $blog_id.'request_button'; ?>" class="j_request_button">



        <?php
        $select_request=mysql_query("SELECT `id` FROM `join_request` WHERE `post_id`='$blog_id' AND `who_request`='$username'");


        if(mysql_num_rows($select_request)>=1){
        echo '<span style="opacity:0.7;cursor:initial">Request Sent</span>';

    }
        else{

        echo '<span style="cursor:pointer">Send Request</span>';

        }
        ?>
        </span>

if I add a alert(inner_text) right before if statement, I can successfully alert "sent request",but I can not alert ok within the if statement 如果我在if语句之前添加警报(inner_text),则可以成功地警报“已发送的请求”,但不能在if语句中警报确定

Try: 尝试:

var inner_text=$.trim($(this).text());

This will remove any surrounding whitespace, which is likely if you have line breaks between your spans. 这将删除周围的空白,如果跨度之间有换行符,则很可能会出现空白。

Don't put a full script like that in your HTML. 不要在HTML中放入像这样的完整脚本。 Give your span an id, and use jQuery in a separate script tag: 给您的跨度一个id,并在单独的脚本标签中使用jQuery:

<span id="sent_request">sent request</span>
<script>
  $('#sent_request').click(function(){
    var inner_text=$(this).text();
    if(inner_text=='sent request'){
      alert('ok'); //not working
    }
  });
</script>

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

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