加载时无法显示元素?

时间:2016-08-18 09:26:12

标签: javascript php jquery ajax

我有问题是当我使用ajax将数据发送到php服务器并让它在视图中显示时,但是数据无法在视图中显示,虽然它在我检入控制台时加载。请帮我解决一下。 这是我的代码:
查看和脚本

<div class="col-xs-12" style="display: block; margin-bottom: 10px;">
<h5 class="col-sm-3 col-sm-offset-1 control-label" style="">Vị trí:</h5>
<div id="positionDiv" class="col-sm-3">
    <select id="position" tabindex="9" name="recruitment[positions]" value="<?php echo $this->data['recruitment']->positions; ?>" data-none-selected-text class="selectpicker">
        <option style="display: none" selected disabled value=""></option>
        <?php foreach($this->data['position'] as $key => $value): ?>
            <option value="<?php echo $value->positions ?>" <?php if ($this->data["recruitment"]->positions == $value->name): ?>selected<?php endif ?>>
                <?php echo $value->name; ?>
            </option>
        <?php endforeach ?>
    </select>
    <input id="positionHidden" type="hidden">
</div>
<script>
    $(document).ready(function() {
        $("#position option").each(function(){
            $(this).siblings("[value='"+ this.value+"']").remove();
        });
        $('#market').on('change', function(event) {
            var value = $('#market').val();
            $.ajax({
                url: window.location.href,
                type: 'POST',
                data: {
                    market: value
                },
                success: function(response){
                    var indexStart = response.indexOf('id="position"') - 8;
                    var indexEnd = response.indexOf('positionHidden') - 49;
                    var str = response.substring(indexStart,indexEnd).replace(/\s\s+/g, ' ');
                    $('#position').remove();
                    $('#positionDiv').append(str);
                    //document.getElementById('positionDiv').innerHTML = str;
                    console.log($('#positionDiv'));
                }
            });

        });
        $.ajaxPrefilter(function(options, originalOptions, jqXHR) {
            options.async = true;
        });                
    });
</script>

控制器

class CandidateController extends BaseController {

    public function __construct($route, $urlValue) {
        parent::__construct($route, $urlValue);   
    }

    public function initData() {
        $this->view->setData("resource", ResourceCompany::getAllResource());
        $this->view->setData("market", Market::getAllMarket());         
    }

    public function showPosition() {
        $market = $_POST['market'];
        isset($market) ? $market : null;

        $this->view->setData("position", Recruitment::getPositionByMarket($market));           
    }

    public function showTitle() {
        isset($market) ? $market : null;
        isset($position) ? $position : null;

        $this->view->setData("title", Recruitment::getTitleByMarketPosition($market, $position));
    }

    public function register() {
        $this->initData();    

        if (isset($_POST['market'])) {
            $this->showPosition();
        }
    }
   $this->view->output("menu");
}

一切都好,数据发送和获取是可以的但它无法在视图中显示,虽然我可以从console.log获取它。

1 个答案:

答案 0 :(得分:0)

mybe你的内容中有特殊字符。 你最好在发送PHP代码之前替换这个字符,并在获得javascript后反向

&#34;&安培;&#34; =&amp;安培;

&#34; \&#34;&#34; =&amp; QUOT;

&#34;&#39;&#34; =&amp;者;

&#34;&LT;&#34; =&amp; LT;

&#34;&GT;&#34; =&amp; GT;

删除&amp;之后的空格plase