douzong5473
2018-05-19 11:25
浏览 236
已采纳

如何从字符串中删除双引号和单引号以及如何传入ajax调用

I need to remove all double quotes,single quotes using jQuery AJAX in with google language translator in PHP when i change onchange() it does not translate the string or character if double quotes found. My code below:

HTML:

<select name="langid" id="langid<@=review_id@>" class="guest clearfix" style="width: 150px;height: 29px;" onchange='translate_text("<@=review_id@>","<@=review_title@>","<@=your_view@>","<@=neg_review@>");'>   
</select>

JavaScript/jQuery:

function translate_text($review_id,$review_title,$your_view,$neg_review) 
{


 var sel = jQuery('#langid'+$review_id).val();
     var url=serverurl+'index.php/feedback';
        showLoadingBar();
        if(sel == 0)
        sel = 'en';



 var passDataAsStr="opcode=translate_text&positivereview="+
$your_view+"&negativereview="+$neg_review+"&title="+$review_title+"&lang="+sel;


            jQuery.ajax({


         type: "POST",
                 url: url,
                //async : false,
                data: passDataAsStr,
                success: function(response) {
                hideLoadingBar();
                // console.log(response);
                    jQuery('#btnclose'+$review_id).show();
                    jQuery('#trans_div'+$review_id).show();
                    var res = jQuery.parseJSON(response);
                    console.log(res['title']);
                    console.log(res['posrev']);
                    console.log(res['negrev']);
                    jQuery('#titleid_'+$review_id).text(res['title']);
                    jQuery('#positiveid'+$review_id).text(res['posrev']);
                    jQuery('#negativeid'+$review_id).text(res['negrev']);

                      }
              });
}

When I want change the language translator it does not change

Suppose $review_title = "New "home" away from home when in village", it does not translate in another languages,because double quotes "home" at the middle of string

Any body having any idea please help to sort it out. Thanks

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutaoer3148 2018-05-19 11:53
    已采纳

    You have two choice:

    Escape

    You can escape double quotes by back slash like this:

    $review_title = "New \"home\" away from home when in village";
    console.log($review_title);

    Replace

    $review_title = 'New "home" away from home when in village';
    $review_title = $review_title.replace(/['"]+/g, "");
    console.log($review_title);

    </div>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题