在`onSelect`中获取在自动完成中选择的数值。

huangapple go评论130阅读模式
英文:

Get value in onSelect which is selected in autocomplete

问题

我正在尝试从AJAX中获取另一个值,但它给我一个错误Uncaught TypeError: e.replace is not a function

以下是我的代码。

source: function(name, response) {
  $.ajax({
    type: "GET",
    dataType: "json",
    url: af_vars.af_ajax_url,
    data: "action=get_listing_names&name=" + name,
    success: function(data) {
      var tickers = [];
      $.map(data, function(item) {
        for (var index = 0; index < item.length; index++) {
          tickers.push(item[index]['tickers']);
        }
      });
      console.log(tickers);
      response(tickers);
    },
    error: function(response) {}
  });
},
onSelect: function(e, term, item) {
  //我的代码
}

这是我的PHP AJAX请求代码。

$titles = array();
if (!empty($results)) {
  foreach ($results as $term) {
    $data = array();
    $data['tickers'] = get_term($term->parent, 'exchange')->name ? esc_attr(get_term($term->parent, 'exchange')->name) . ' : ' . addslashes($term->name) : addslashes($term->name);
    $data['isArchived'] = get_term_meta($term->term_id, 'ticker_archived', true) == 1 ? 'true' : 'false';
    $titles[] = $data;
  }
} else {
  $titles[] = "No results found";
}
echo json_encode(array(
  'results' => $titles
));

因此,我想在下拉菜单中显示股票代码(tickers),并希望将isArchive结果作为隐藏值传递,以便在onSelect中一起传递。

英文:

I am trying to catch another value from AJAX but it is giving me an error Uncaught TypeError: e.replace is not a function.

Here is my code.

     source: function(name, response) {
      $.ajax({
        type: &quot;GET&quot;,
        dataType: &quot;json&quot;,
        url: af_vars.af_ajax_url,
        data: &quot;action=get_listing_names&amp;name=&quot; + name,
        success: function(data) {
         // response(data);
            var tickers = [];
            $.map( data, function( item ) {
                for (var index = 0; index &lt; item.length; index++) {
                    tickers.push(item[index][&#39;tickers&#39;]);
                }
            });
            console.log(tickers);
            response(tickers);
        },
        error: function(response) {}
      });
    },
    onSelect: function(e, term, item) {
      //my code
    }

and here is my PHP ajax request code

$titles = array();
    if (!empty($results)) {
        foreach ($results as $term) {
            $data = array();
            $data[&#39;tickers&#39;] = get_term( $term-&gt;parent, &#39;exchange&#39; )-&gt;name ? esc_attr( get_term( $term-&gt;parent, &#39;exchange&#39; )-&gt;name ) .&#39; : &#39;. addslashes($term-&gt;name) : addslashes($term-&gt;name) ;
            $data[&#39;isArchived&#39;]= get_term_meta( $term-&gt;term_id , &#39;ticker_archived&#39;, true) == 1 ? &#39;true&#39; : &#39;false&#39;;
            $titles[] = $data;
        }
    }else {
        $titles[] = &quot;No results found&quot;;        
    }      
    echo json_encode(array(
        &#39;results&#39; =&gt; $titles
        )
    ); 

So, I want to show tickers in dropdown and want isArchive result as hidden value to pass along with tickers in onSelect

答案1

得分: 2

你可以尝试这种方式,对我来说有效。或者如果你能将它放在服务器上并给我访问权限,那么我也可以在那里检查。

source: function(query, process) {
    var objects = [];
    map = {};
    $.ajax({
        url: baseURL + "/admin/getmember",
        data: 'q=' + query,
        dataType: "json",
        type: "GET",
        success: function (data) {
            console.log(data);
            $.each(data, function(i, object) {
                map[object.membername] = object;
                objects.push(object.membername);
            });
            process(objects);
        }
    });
},
updater: function(item) {
    $('#hiddenId').val(map[item].id);
    return item;
}

PHP代码:

$memberResults = array();
foreach ($members as $k => $member) {
    $memberResults[$k]['id'] = $member->id;
    $memberResults[$k]['membername'] = $member->fname . ' ' . $member->lname;
}
英文:

You can try this way it is working for me. or if you can put it on server and give me access so i can check that there as well as.

source: function(query, process) {
	var objects = [];
	map = {};
	$.ajax({
		url: baseURL+&quot;/admin/getmember&quot;,
		data: &#39;q=&#39; + query,            
		dataType: &quot;json&quot;,
		type: &quot;GET&quot;,
		success: function (data) {
			console.log(data);
			$.each(data, function(i, object) {
				map[object.membername] = object;
				objects.push(object.membername);
			});
			process(objects); 
		}
	}); 
},
updater: function(item) {
	$(&#39;#hiddenId&#39;).val(map[item].id);
	return item;
}

PHP code

$memberResults = array();
foreach($members as $k=&gt;$member){
	$memberResults[$k][&#39;id&#39;] = $member-&gt;id;
	$memberResults[$k][&#39;membername&#39;] = $member-&gt;fname.&#39; &#39;.$member-&gt;lname;
}

答案2

得分: 2

你可以注册任何变化:

$('html').bind('input', function() {
    alert('test');
});

另一个选项是在失去焦点时(离开元素焦点)进行更新:

$("input").on('blur keyup change click', function () {
英文:

You could register to any change:

$(&#39;html&#39;).bind(&#39;input&#39;, function() {
    alert(&#39;test&#39;);
});

Other option is to get update on blur (leaving focus of element)

$(&quot;input&quot;).on(&#39;blur keyup change click&#39;, function () {

答案3

得分: 1

"Uncaught TypeError: e.replace is not a function" 意味着 "变量 'e' 不是一个字符串。"

除此之外.......由于变量 "e" 用在写作为 " //我的代码 " 的部分,你没有分享代码中最重要的部分(包含错误的部分在这里不可见)。

你能展示一下 "OnSelect" 函数内部的内容吗?或者给我们一个 CodePen?

英文:

"Uncaught TypeError: e.replace is not a function" means "The variable 'e' is not a string."

Beyond that.......Being that the variable "e" is used in the part that's written as " //my code ", you aren't sharing the most important part of your code (The part that contains an error isn't visible here).

Can you show us what's inside the "OnSelect" function? Perhaps give us a codepen?

huangapple
  • 本文由 发表于 2020年1月3日 18:54:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定