![](/img/trans.png)
[英]Detect change of value of struts2 autocompleter tag using javascript or jquery
[英]Reading selected value from struts2 jquery autocompleter using javascript
我有一個jsp頁面和一個struts2-jquery自動完成器。 自動完成程序會加載值,每當我選擇一個值時,我都希望將該值檢索到JavaScript函數中。
我的代碼如下。 但這是行不通的。
JavaScript代碼:
function loadAjax(){
var empId=document.forms[0].employeename.value;
alert("Value "+empId);
}
自動完成程序代碼:
<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()事件不適用於JQuery Autocompleter。 嘗試使用按鈕及其onclick()事件。
<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>
上面的代碼未經測試,並使用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"/>
在您的javascript部分中添加以下提及的代碼。
<script type="text/javascript">
$.subscribe('loadAjax', function(event, data)
{
var empId = data.value;
alert(empId);
});
</script>
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.