简体   繁体   English

如何将单击的单选按钮的值传递给Spring MVC控制器?

[英]How to pass value of radio button which is clicked to Spring MVC controller?

I am using Spring MVC controller in one of my project. 我在我的一个项目中使用Spring MVC控制器。

Below is my JSP code in which it will show three radio button. 下面是我的JSP代码,它将在其中显示三个单选按钮。 One is Insert , second radio button is Update and third radio button is Delete . 一个是Insert ,第二个单选按钮是Update ,第三个单选按钮是Delete

Once I click Insert radio button, it shows two text box next to Insert radio button, and same thing with other two radio button as well. 单击“ Insert单选按钮后,它会在“ Insert单选按钮旁边显示两个文本框,同样也会显示其他两个单选按钮。 Here is my jsfiddle 这是我的jsfiddle

<script type="text/javascript">
    $(document).ready(function(){
        $(".changeAction").on("click", function(){
            $('.changeable').html('')
            var divId = "#" + $(this).attr("div-id");
            var myInput = '<label for="Node"> Node </label> <input type="text" name="node" size="20" /> <label for="Data"> Data </label> <input type="text" name="data" size="100"/>'
            $(divId).html(myInput);
        })
    })      
</script>

<body>
<form method="post" action="testOperation">
    <!-- I used only one hidden box to store value -->
    <input type="hidden" name="name" id="dynamicName">
    <input class="changeAction" type="radio" name="tt" value="Insert" div-id="insert"/> Insert
    <div id="insert" class="changeable"></div>
    <br/> <input class="changeAction" type="radio" name="tt" value="Update" div-id="update"/> Update
    <div id="update" class="changeable"></div>
    <br/> <input class="changeAction" type="radio" name="tt" value="Delete" div-id="delete"/> Delete
    <div id="delete" class="changeable"></div>
    <br/>
    <input type="submit">
</form>
</body>

Below is my method in Controller code - 下面是我在Controller代码中的方法 -

   @RequestMapping(value = "testOperation", method = RequestMethod.GET)
    public Map<String, String> testOperation() {
        final Map<String, String> model = new LinkedHashMap<String, String>();
        return model;
    }

    @RequestMapping(value = "testOperation", method = RequestMethod.POST)
    public Map<String, String> testOperations(@RequestParam String name, 
                                              @RequestParam String node, 
                                              @RequestParam String data) {
        final Map<String, String> model = new LinkedHashMap<String, String>();

        System.out.println(name);
        System.out.println(node);
        System.out.println(data);

        return model;
    }

Problem Statement:- 问题陈述:-

Suppose I click Insert radio button and type Hello in the first text and World in the second text box, then I will hit the submit button, and after that I am seeing Hello value in node variable and World value in data variable which is correct. 假设我单击Insert单选按钮并在第一个文本中输入Hello ,在第二个文本框中输入World ,然后我将点击提交按钮,之后我看到node变量中的Hello值和data变量中的World值是正确的。

But somehow, name variable is coming as empty instead of that it should show insert value as I have clicked insert radio button. 但不知何故, name变量是空的而不是它应该显示insert值,因为我点击插入单选按钮。

And same thing should happen to update and delete radio button.. 同样的事情应该发生更新和删除单选按钮..

Any thoughts what wrong I am doing? 有什么想法,我在做什么错?

Replace Your Javascript by this. 用这个替换你的Javascript。 It worked for me. 它对我有用。

$(document).ready(function(){
    $(".changeAction").on("click", function(){
        $('.changeable').html('')
        var divId = $(this).attr("div-id");
        $("#dynamicName").val(divId);
        divId = "#"+divId;
        var myInput = '<label for="Node"> Node </label> <input type="text" name="node" size="20" /> <label for="Data"> Data </label> <input type="text" name="data" size="100"/>'
        $(divId).html(myInput);
    })
})

here is jsfiddle http://jsfiddle.net/EGJk8/5/ 这里是jsfiddle http://jsfiddle.net/EGJk8/5/

Change the code as, 将代码更改为,

From , 来自,

@RequestParam String name

To , 至 ,

@RequestParam String tt

Because you have mentioned the name of the radio button as tt . 因为您已经将单选按钮的名称称为tt So can't get the value like Insert , Update , Delete . 所以无法获得像Insert , Update , Delete这样的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM