[英]Reading selected value from struts2 jquery autocompleter using javascript
I have a jsp page and a struts2-jquery autocompleter in it. 我有一个jsp页面和一个struts2-jquery自动完成器。 The autocompleter is loaded with values and whenever I select a value I want to retrieve that value to a JavaScript function.
自动完成程序会加载值,每当我选择一个值时,我都希望将该值检索到JavaScript函数中。
My code is given below. 我的代码如下。 But it is not working.
但这是行不通的。
JavaScript code: JavaScript代码:
function loadAjax(){
var empId=document.forms[0].employeename.value;
alert("Value "+empId);
}
Autocompleter code: 自动完成程序代码:
<sj:autocompleter name="employeename" id="employee" label="Employee ID" list="employeeMap" onchange="loadAjax();"/>
<html>
<head>
<sx:head />
</head>
<body>
<h1>Struts 2 autocompleter + JSON example</h1>
<s:form action="resultAction" namespace="/" method="POST" >
<s:url id="databaseList" action="databaseJSON" />
<sx:autocompleter label="What's your favorite Database Server?"
href="%{databaseList}" name="yourFavDatabase" />
<s:submit value="submit" name="submit" />
</s:form>
</body>
</html>
onchange() event not working with JQuery Autocompleter. onchange()事件不适用于JQuery Autocompleter。 Try using a button and its onclick() event.
尝试使用按钮及其onclick()事件。
Use onSelectTopics 使用onSelectTopics
<sj:autocompleter name="employeename" id="employee" label="Employee ID" list="employeeMap" onSelectTopics="auto-select"/>
<script>
$(function(){
$.subscribe("auto-select",function(event){
console.log(event.originalEvent.event);
console.log(event.originalEvent.ui);
console.log(event.originalEvent.ui.item.desc);
console.log(event.originalEvent.ui.item.value);
});
});
</script>
Above code is untested and written using reference from jquery-ui 上面的代码未经测试,并使用jquery-ui的引用编写
$( "#project" ).val( ui.item.label );
$( "#project-id" ).val( ui.item.value );
$( "#project-description" ).html( ui.item.desc );
$( "#project-icon" ).attr( "src", "images/" + ui.item.icon );
<sj:autocompleter
id="employee" name="employeename" label="Employee ID"
list="employeeMap" onSelectTopics="loadAjax"/>
Add below mention code in you javascript section. 在您的javascript部分中添加以下提及的代码。
<script type="text/javascript">
$.subscribe('loadAjax', function(event, data)
{
var empId = data.value;
alert(empId);
});
</script>
Regards 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.