gpt4 book ai didi

jquery - 如何获得对同一GSP页面的回复

转载 作者:行者123 更新时间:2023-12-02 15:49:24 24 4
gpt4 key购买 nike

我有两个选择下拉框,选择值并单击“提交”后,我需要从数据库中检索值,并检查它们是否相同(如果相同),则需要在同一gsp页中显示一条消息,即“两个值”一样”。如果它们不相等,那么我应该在同一gsp页面的文本区域中打印检索到的值。所有这些都应显示在同一gsp页面上

This is my gsp page

<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main" />
<title>Render Domain</title>

<g:javascript plugin="jquery" library="jquery" src="jquery/jquery-1.7.2.js"/>
<script>
$(document).ready(function(){
$('.testMe').click(function(){
var URL="${createLink(controller:'jsonComparison',action:'compare')}";
alert(URL)
alert(asd.value)
alert(asd1.value)
$.ajax({
url:URL,
data: {asd:asd.value,asd1:asd1.value},
success: function(data){
//console.log(resp);
$("#asd").val(resp.asd);
$("#asd").val(resp.asd);
}
});
});
});
</script>


</head>
<body>
<g:form>
<g:select name="asd" from="${eventsList}" value="1" noSelection="['':'-Choose the From Date-']"/>
<g:select name="asd1" from="${eventsList}" value="1" noSelection="['':'-Choose the From Date-']"/>
<%-- <g:actionSubmit value="Compare" action="compare" /> --%>
<button class="testMe">Compare</button>
</g:form>
</body>
</html>

This is my controller method
def compare(){
println "compare method called"
def values = params.asd
def values1 = params.asd1
println "first value"+ values
println "second value"+values1
println "form submitted successfully"
if(values !=null && values1 !=null){
render (view:"index")
}
else{
println "values are same"
}

}

任何人都可以帮助我解决该问题,因为我是新来的人,对如何做到这一点感到震惊

最佳答案

我添加了一个最小的变化的小例子。我认为这只是一个学习任务,因为此架构不适用于实际的解决方案。

ComareController:

class CompareController {

def index() {
def eventsList = ['event1', 'event2', 'event3']
render view: 'index', model: [eventsList: eventsList]
}

def ajaxCompare() {
//You can retrieve entries from DB here like Event.get(params.long('firstEventId'))
//But it will be better to add EventService to interact with DB
render status: 200, text: params.firstEvent == params.secondEvent ? "Equal" : "Different: ${params.firstEvent} != ${params.secondEvent}"
}
}

比较/index.gsp:
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title> Render Domain </title>
<g:javascript plugin="jquery" library="jquery" src="jquery/jquery-1.11.1.js"/>
<script>
$(document).ready(function(){
$('#doCompare').click(function(){
$.ajax({
url: "${createLink(controller:'compare', action:'ajaxCompare')}",
data: {firstEvent: $('#firstEvent').val(), secondEvent: $('#secondEvent').val()},
success: function(data){
$('#result').val(data)
}
});
});
});
</script>
</head>

<body>
<g:select name="firstEvent" from="${eventsList}" value="${eventsList.first()}" noSelection="['':'-Choose the From Date-']"/>
<g:select name="secondEvent" from="${eventsList}" value="${eventsList.last()}" noSelection="['':'-Choose the From Date-']"/>
<button id="doCompare" onclick="void(0);">Compare</button>
<br>
<textarea id="result"></textarea>
</body>
</html>

关于jquery - 如何获得对同一GSP页面的回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42083892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com