简体   繁体   English

使用JavaScript从Struts2 jQuery自动完成器读取所选值

[英]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.

相关问题 使用 javascript 或 jquery 检测 struts2 自动完成标记值的变化 - Detect change of value of struts2 autocompleter tag using javascript or jquery 如何使用从另一个struts2-jquery自动完成程序Y中选择的值重新加载struts2-jquery自动完成程序X - How to reload the struts2-jquery autocompleter X with value selected from another struts2-jquery autocompleter Y Struts2 jQuery自动完成器未转义ASCII - Struts2 jQuery Autocompleter Not Escaping ASCII Struts2:基于同一JSP中自动完成者字段中的选定值填充表(来自数据库) - Struts2: populating a table (from database) based on selected values from autocompleter fields in the same JSP 如何在Struts2自动完成操作中传递javascript对象? - How to pass javascript object in Struts2 autocompleter action? struts2: update second select based on first select value using javascript and jquery - struts2: update second select based on first select value using javascript and jquery 在JavaScript中读取Struts2标签值 - reading Struts2 tag values in javascript 使用JavaScript更改struts2迭代器内的输入值 - Change value of input inside an struts2 iterator using javascript 将值从jsp中的javascript传递到struts2中的动作类 - Pass value from javascript in jsp to action class in struts2 在使用struts2选择一个下拉值时从数据库中检索值 - Retrieving values from database on selection a dropdown value using struts2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM